Added Response DTO

4.x.x
Chatura Dilan 8 years ago
parent 7f6b33a0c0
commit 1095151085

@ -132,6 +132,10 @@
<artifactId>commons-httpclient</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>

@ -0,0 +1,88 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.application.mgt.api.dto;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.wso2.carbon.device.application.mgt.core.dto.Application;
import java.util.List;
public class StoreApplication {
private int id;
private String uuid;
private String iconName;
private String bannerName;
private List<String> screenshotNames;
private Application application;
@JsonIgnore
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getIconName() {
return iconName;
}
public void setIconName(String iconName) {
this.iconName = iconName;
}
public String getBannerName() {
return bannerName;
}
public void setBannerName(String bannerName) {
this.bannerName = bannerName;
}
public List<String> getScreenshotNames() {
return screenshotNames;
}
public void setScreenshotNames(List<String> screenshotNames) {
this.screenshotNames = screenshotNames;
}
public Application getApplication() {
return application;
}
public void setApplication(Application application) {
this.application = application;
}
}

@ -0,0 +1,59 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.application.mgt.api.responses;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.beanutils.BeanUtils;
import org.wso2.carbon.device.application.mgt.api.dto.StoreApplication;
import org.wso2.carbon.device.application.mgt.core.dto.Application;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
@XmlRootElement
public class ApplicationsListResponse {
@ApiModelProperty(value = "List of applications types returned")
@JsonProperty("applications")
@XmlElement
private List<StoreApplication> applications;
public ApplicationsListResponse(List<org.wso2.carbon.device.application.mgt.core.dto.StoreApplication> applications)
throws InvocationTargetException, IllegalAccessException {
this.applications = new ArrayList<>();
for(org.wso2.carbon.device.application.mgt.core.dto.StoreApplication applicationDTO : applications){
StoreApplication application = new StoreApplication();
BeanUtils.copyProperties(application, applicationDTO);
this.applications.add(application);
}
}
public List<StoreApplication> getApplications() {
return applications;
}
public void setApplications(List<StoreApplication> applications) {
this.applications = applications;
}
}

@ -20,12 +20,13 @@ package org.wso2.carbon.device.application.mgt.api.services;
import io.swagger.annotations.*;
import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse;
import org.wso2.carbon.device.application.mgt.api.responses.ApplicationsListResponse;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/applications")
@Api(value = "Application Management", description = "This API carries all device management related operations " +
"such as get all the available devices, etc.")
@Produces(MediaType.APPLICATION_JSON)
@ -34,53 +35,13 @@ public interface ApplicationManagementService {
public final static String SCOPE = "scope";
@POST
@Path("/")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
@ApiOperation(
consumes = MediaType.TEXT_PLAIN,
produces = MediaType.TEXT_PLAIN,
httpMethod = "POST",
value = "Creates new application",
notes = "This will create a new application",
tags = "Application Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:create-application")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully fetched the device location.",
response = String.class),
@ApiResponse(
code = 304,
message = "Not Modified. \n " +
"Empty body because the client already has the latest version of the requested resource."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while retrieving signed certificate.",
response = ErrorResponse.class)
})
Response createApplication(
@ApiParam(
name = "If-Modified-Since",
value = "Validates if the requested variant has not been modified since the time specified",
required = false)
@HeaderParam("If-Modified-Since") String ifModifiedSince,
String name);
@GET
@Path("/")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
@Path("applications")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.TEXT_PLAIN,
produces = MediaType.TEXT_PLAIN,
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "get all applications",
notes = "This will get all applications",
@ -96,7 +57,7 @@ public interface ApplicationManagementService {
@ApiResponse(
code = 200,
message = "OK. \n Successfully got application list.",
response = String.class),
response = ApplicationsListResponse.class),
@ApiResponse(
code = 304,
message = "Not Modified. \n " +
@ -106,7 +67,7 @@ public interface ApplicationManagementService {
message = "Internal Server Error. \n Error occurred while getting the application list.",
response = ErrorResponse.class)
})
Response getApplications(
ApplicationsListResponse getApplications(@Context final HttpServletResponse servletResponse,
@ApiParam(
name = "If-Modified-Since",
value = "Validates if the requested variant has not been modified since the time specified",

@ -18,38 +18,46 @@
*/
package org.wso2.carbon.device.application.mgt.api.services.impl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse;
import org.wso2.carbon.device.application.mgt.api.services.ApplicationManagementService;
import org.wso2.carbon.device.application.mgt.api.responses.ApplicationsListResponse;
import org.wso2.carbon.device.application.mgt.core.components.ApplicationManager;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException;
import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil;
import javax.ws.rs.POST;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
public class ApplicationManagementServiceImpl implements ApplicationManagementService {
@Produces({ "application/json"})
@Consumes({ "application/json"})
public class ApplicationManagementServiceImpl {
private static Log log = LogFactory.getLog(ApplicationManagementServiceImpl.class);
@POST
@Override
public Response createApplication(String ifModifiedSince, String name) {
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManager();
return null;
}
@Override
public Response getApplications(String ifModifiedSince) {
@GET
@Consumes("application/json")
@Path("applications")
public ApplicationsListResponse getApplications(@Context final HttpServletResponse servletResponse) {
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManager();
try {
return Response.ok().entity(applicationManager.getApplications()).build();
} catch (ApplicationManagerException e) {
String msg = "Error occured while getting the application list";
ApplicationsListResponse applicationsListResponse =
new ApplicationsListResponse(applicationManager.getApplications());
return applicationsListResponse;
} catch (Exception e) {
String msg = "Error occurred while getting the application list";
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
try {
servletResponse.sendError(Response.Status.NOT_FOUND.getStatusCode());
} catch (IOException e1) {
log.error(msg, e1);
}
return null;
}
}
}

@ -1,11 +1,11 @@
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<jaxrs:server id="applicationMgtService" address="/">
<jaxrs:serviceBeans>
@ -13,12 +13,10 @@
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider"/>
<ref bean="errorHandler"/>
</jaxrs:providers>
</jaxrs:server>
<bean id="applicationMgtServiceBean" class="org.wso2.carbon.device.application.mgt.api.services.impl.ApplicationManagementServiceImpl"/>
<bean id="jsonProvider" class="org.wso2.carbon.device.application.mgt.api.common.GsonMessageBodyHandler"/>
<bean id="errorHandler" class="org.wso2.carbon.device.application.mgt.api.common.ErrorHandler"/>
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
</beans>

@ -82,6 +82,7 @@
org.wso2.carbon.context,
org.jscep.transaction,
org.w3c.dom,
org.json,
org.xml.sax,
javax.sql,
<!--javax.cache,-->
@ -187,6 +188,10 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json.wso2</groupId>
<artifactId>json</artifactId>
</dependency>
</dependencies>
</project>

@ -19,6 +19,7 @@
package org.wso2.carbon.device.application.mgt.core.components;
import org.wso2.carbon.device.application.mgt.core.dto.Application;
import org.wso2.carbon.device.application.mgt.core.dto.StoreApplication;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException;
import java.util.ArrayList;
@ -28,6 +29,6 @@ public interface ApplicationManager {
public void createApplication(Application application) throws ApplicationManagerException;
public List<Application> getApplications() throws ApplicationManagerException;
public List<StoreApplication> getApplications() throws ApplicationManagerException;
}

@ -25,6 +25,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAO;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dto.Application;
import org.wso2.carbon.device.application.mgt.core.dto.StoreApplication;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException;
import org.wso2.carbon.device.application.mgt.core.internal.ApplicationManagementDataHolder;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
@ -55,10 +56,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
@Override
public List<Application> getApplications() throws ApplicationManagerException {
public List<StoreApplication> getApplications() throws ApplicationManagerException {
ConnectionManagerUtil.openConnection();
ApplicationManagementDAO applicationManagementDAO = ApplicationManagementDataHolder.getInstance().getApplicationManagementDAO();
List<Application> applications = null;
List<StoreApplication> applications = null;
applications = applicationManagementDAO.getApplications();
ConnectionManagerUtil.closeConnection();
return applications;

@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.dao;
import org.apache.axis2.databinding.types.Day;
import org.wso2.carbon.device.application.mgt.core.dto.Application;
import org.wso2.carbon.device.application.mgt.core.dto.StoreApplication;
import java.util.HashMap;
import java.util.List;
@ -57,8 +58,8 @@ public interface ApplicationManagementDAO {
}
}
public void createApplication(Application application) throws ApplicationManagementDAOException;
public void createApplication(StoreApplication application) throws ApplicationManagementDAOException;
public List<Application> getApplications() throws ApplicationManagementDAOException;
public List<StoreApplication> getApplications() throws ApplicationManagementDAOException;
}

@ -20,7 +20,9 @@ package org.wso2.carbon.device.application.mgt.core.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.wso2.carbon.device.application.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.application.mgt.core.dto.StoreApplication;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.application.mgt.core.dto.Application;
@ -37,6 +39,8 @@ public class ApplicationManagementDAOImpl implements ApplicationManagementDAO {
private DatabaseType databaseType;
private static DataSource dataSource;
private static final String TABLE_PREFIX = "APPM";
private static final Log log = LogFactory.getLog(ApplicationManagementDAOImpl.class);
public ApplicationManagementDAOImpl(DataSourceConfig dataSourceConfig) {
@ -52,25 +56,25 @@ public class ApplicationManagementDAOImpl implements ApplicationManagementDAO {
}
@Override
public void createApplication(Application application) throws ApplicationManagementDAOException {
public void createApplication(StoreApplication application) throws ApplicationManagementDAOException {
}
@Override
public List<Application> getApplications() throws ApplicationManagementDAOException {
public List<StoreApplication> getApplications() throws ApplicationManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = null;
List<Application> applications;
List<StoreApplication> applications;
try {
conn = ConnectionManagerUtil.getCurrentConnection().get();
switch (databaseType) {
case H2:
case MYSQL:
sql = "SELECT * FROM APPM_APPLICATION";
sql = "SELECT * FROM APPM_STORE_APPLICATION";
}
stmt = conn.prepareStatement(sql);
@ -82,6 +86,8 @@ public class ApplicationManagementDAOImpl implements ApplicationManagementDAO {
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
} catch (JSONException e) {
throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e);
} finally {
ApplicationManagementDAOUtil.cleanupResources(stmt, rs);
}

@ -20,26 +20,30 @@ package org.wso2.carbon.device.application.mgt.core.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.wso2.carbon.device.application.mgt.core.dto.Application;
import org.wso2.carbon.device.application.mgt.core.dto.ApplicationType;
import org.wso2.carbon.device.application.mgt.core.dto.StoreApplication;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ApplicationManagementDAOUtil {
private static final Log log = LogFactory.getLog(ApplicationManagementDAOUtil.class);
public static Application loadApplication(ResultSet rs) throws SQLException {
public static StoreApplication loadApplication(ResultSet rs) throws SQLException, JSONException {
ApplicationType applicationType = new ApplicationType();
Application application = new Application();
application.setId(rs.getInt("ID"));
application.setName(rs.getString("NAME"));
application.setUuId(rs.getString("UUID"));
application.setDescription(rs.getString("DESCRIPTION"));
applicationType.setId(rs.getInt("APPLICATION_TYPE_ID"));
application.setApplicationType(applicationType);
return application;
StoreApplication storeApplication = new StoreApplication();
storeApplication.setUuid(rs.getString("UUID"));
storeApplication.setIconName(rs.getString("ICON_NAME"));
storeApplication.setBannerName(rs.getString("BANNER_NAME"));
storeApplication.setScreenshotNames(jsonArrayStringToList(rs.getString("SCREENSHOTS")));
return storeApplication;
}
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
@ -58,4 +62,16 @@ public class ApplicationManagementDAOUtil {
}
}
}
public static List<String> jsonArrayStringToList(String value) throws JSONException {
JSONArray jsonArray = new JSONArray(value);
List<String> list = new ArrayList<>();
if (jsonArray != null) {
int len = jsonArray.length();
for (int i = 0; i < len; i++) {
list.add(jsonArray.get(i).toString());
}
}
return list;
}
}

@ -18,16 +18,15 @@
*/
package org.wso2.carbon.device.application.mgt.core.dto;
import java.util.List;
import java.util.Map;
public class Application{
public class Application {
private int id;
private String uuId;
private String name;
private String description;
private ApplicationType applicationType;
private List<String> properties;
private Map<String, String> properties;
public int getId() {
@ -38,14 +37,6 @@ public class Application{
this.id = id;
}
public String getUuId() {
return uuId;
}
public void setUuId(String uuId) {
this.uuId = uuId;
}
public String getName() {
return name;
}
@ -70,11 +61,11 @@ public class Application{
this.applicationType = applicationType;
}
public List<String> getProperties() {
public Map<String, String> getProperties() {
return properties;
}
public void setProperties(List<String> properties) {
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
}

@ -20,9 +20,9 @@ package org.wso2.carbon.device.application.mgt.core.dto;
public class ApplicationType {
private int id;
private String name;
private String code;
protected int id;
protected String name;
protected String code;
public int getId() {
return id;

@ -0,0 +1,79 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.application.mgt.core.dto;
import java.util.List;
public class StoreApplication {
private int id;
private String uuid;
private String iconName;
private String bannerName;
private List<String> screenshotNames;
private Application application;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getIconName() {
return iconName;
}
public void setIconName(String iconName) {
this.iconName = iconName;
}
public String getBannerName() {
return bannerName;
}
public void setBannerName(String bannerName) {
this.bannerName = bannerName;
}
public List<String> getScreenshotNames() {
return screenshotNames;
}
public void setScreenshotNames(List<String> screenshotNames) {
this.screenshotNames = screenshotNames;
}
public Application getApplication() {
return application;
}
public void setApplication(Application application) {
this.application = application;
}
}
Loading…
Cancel
Save