Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiyamSanthosh committed Jan 24, 2025
1 parent f9dbb99 commit 645f58a
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,4 @@
</plugins>
</build>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# Uncomment this line if you want to override the impl classes.
**/impl/*

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@
<description>WSO2 Identity Server - Push Notification Device Management REST API</description>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
Expand All @@ -51,7 +45,7 @@
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.1.2</version>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<goals>
Expand Down Expand Up @@ -171,4 +165,4 @@
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@

package org.wso2.carbon.identity.rest.api.user.push.v1.core;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.api.user.common.ContextLoader;
import org.wso2.carbon.identity.api.user.common.error.APIError;
import org.wso2.carbon.identity.api.user.common.error.ErrorResponse;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.notification.push.device.handler.DeviceHandlerService;
import org.wso2.carbon.identity.notification.push.device.handler.exception.PushDeviceHandlerClientException;
import org.wso2.carbon.identity.notification.push.device.handler.exception.PushDeviceHandlerException;
import org.wso2.carbon.identity.notification.push.device.handler.model.Device;
import org.wso2.carbon.identity.notification.push.device.handler.model.RegistrationDiscoveryData;
Expand All @@ -38,16 +39,17 @@
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.Response;

import static org.wso2.carbon.identity.notification.push.device.handler.constant.PushDeviceHandlerConstants.ErrorMessages.ERROR_CODE_DEVICE_NOT_FOUND_FOR_USER_ID;
import static org.wso2.carbon.identity.rest.api.user.push.v1.util.Util.handlePushDeviceHandlerException;

/**
* Service class for push device management.
*/
public class PushDeviceManagementService {

private final DeviceHandlerService deviceHandlerService;
private static final Log log = LogFactory.getLog(PushDeviceManagementService.class);
private static final Log LOG = LogFactory.getLog(PushDeviceManagementService.class);

/**
* Constructor for PushDeviceManagementService.
Expand All @@ -64,16 +66,14 @@ public PushDeviceManagementService(DeviceHandlerService deviceHandlerService) {
*
* @return Registration discovery data.
*/
public String getRegistrationDiscoveryData() {
public DiscoveryDataDTO getRegistrationDiscoveryData() {

User user = ContextLoader.getUserFromContext();
String username = user.toFullQualifiedUsername();
String tenantDomain = user.getTenantDomain();
try {
RegistrationDiscoveryData data = deviceHandlerService.getRegistrationDiscoveryData(username, tenantDomain);
DiscoveryDataDTO discoveryDataDTO = buildDiscoveryDataDTO(data);
Gson gson = new GsonBuilder().create();
return gson.toJson(discoveryDataDTO);
return buildDiscoveryDataDTO(data);
} catch (PushDeviceHandlerException e) {
throw handlePushDeviceHandlerException(e);
}
Expand Down Expand Up @@ -110,6 +110,7 @@ public List<DeviceDTO> getDeviceByUserId() {
Device device = deviceHandlerService.getDeviceByUserId(userId, tenantDomain);
deviceDTOList.add(buildDeviceDTO(device));
} catch (PushDeviceHandlerException e) {
// If no device registered for the userId, the below-mentioned error is thrown.
if (!ERROR_CODE_DEVICE_NOT_FOUND_FOR_USER_ID.getCode().equals(e.getErrorCode())) {
throw handlePushDeviceHandlerException(e);
}
Expand Down Expand Up @@ -216,4 +217,31 @@ private RegistrationRequest buildRegistrationRequest(RegistrationRequestDTO dto)
request.setSignature(dto.getSignature());
return request;
}

/**
* Handle push device handler exception.
*
* @param e PushDeviceHandlerException.
* @return APIError.
*/
private static APIError handlePushDeviceHandlerException(PushDeviceHandlerException e) {

if (e instanceof PushDeviceHandlerClientException) {
ErrorResponse errorResponse = getErrorBuilder(e).build(LOG, e.getMessage());
return new APIError(Response.Status.BAD_REQUEST, errorResponse);
}
ErrorResponse errorResponse = getErrorBuilder(e).build(LOG, e, e.getMessage());
return new APIError(Response.Status.INTERNAL_SERVER_ERROR, errorResponse);
}

/**
* Get error builder.
*
* @param e PushDeviceHandlerException.
* @return ErrorResponse.Builder.
*/
private static ErrorResponse.Builder getErrorBuilder(PushDeviceHandlerException e) {

return new ErrorResponse.Builder().withCode(e.getErrorCode()).withMessage(e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

package org.wso2.carbon.identity.rest.api.user.push.v1.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.wso2.carbon.identity.rest.api.user.push.v1.DiscoveryDataApiService;
import org.wso2.carbon.identity.rest.api.user.push.v1.core.PushDeviceManagementService;

import javax.ws.rs.core.Response;
import org.wso2.carbon.identity.rest.api.user.push.v1.factories.PushDeviceManagementServiceFactory;
import org.wso2.carbon.identity.rest.api.user.push.v1.model.DiscoveryDataDTO;

/**
* Implementation class of Push device Handler User APIs.
Expand All @@ -44,7 +44,7 @@ public DiscoveryDataApiServiceImpl() {
@Override
public Response getRegistrationDiscoveryData() {

String responseDTO = pushDeviceManagementService.getRegistrationDiscoveryData();
return Response.ok().entity(responseDTO).build();
DiscoveryDataDTO discoveryDataDTO = pushDeviceManagementService.getRegistrationDiscoveryData();
return Response.ok().entity(discoveryDataDTO).build();
}
}

This file was deleted.

Loading

0 comments on commit 645f58a

Please sign in to comment.