First cut of refactoring the code.

feature/appm-store/pbac
sinthuja 8 years ago
parent 9343032c88
commit dd6e70ce86

@ -0,0 +1,68 @@
/*
* 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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.core.services.impl.ApplicationManagementServiceFactory;
import javax.ws.rs.core.Response;
/**
* Holds util methods required for Application-Mgt API component.
*/
public class APIUtil {
private static Log log = LogFactory.getLog(APIUtil.class);
private static ApplicationManagementServiceFactory applicationManagementServiceFactory;
public static ApplicationManagementServiceFactory getApplicationManagementServiceFactory() {
if (applicationManagementServiceFactory == null) {
synchronized (APIUtil.class) {
if (applicationManagementServiceFactory == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
applicationManagementServiceFactory =
(ApplicationManagementServiceFactory) ctx.getOSGiService(ApplicationManagementServiceFactory.class, null);
if (applicationManagementServiceFactory == null) {
String msg = "Application Management provider service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
}
}
}
return applicationManagementServiceFactory;
}
public static Response getResponse(ApplicationManagementException ex, Response.Status status) {
//TODO: check for exception type and set the response code.
ErrorResponse errorMessage = new ErrorResponse();
errorMessage.setMessage(ex.getMessage());
if (status == null) {
status = Response.Status.INTERNAL_SERVER_ERROR;
}
errorMessage.setCode(status.getStatusCode());
return Response.status(status).entity(errorMessage).build();
}
}

@ -21,8 +21,9 @@ import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ApiOriginFilter implements Filter {
public class APIOriginFilter implements Filter {
//TODO: check no config option.
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) response;

@ -16,7 +16,7 @@
* under the License.
*/
package org.wso2.carbon.device.application.mgt.api.common;
package org.wso2.carbon.device.application.mgt.api;
import com.google.gson.Gson;
@ -40,7 +40,7 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
@Provider
@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
public class JSONMessageHandler implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
private Gson gson;
private static final String UTF_8 = "UTF-8";
@ -62,13 +62,8 @@ public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, Messag
public Object readFrom(Class<Object> objectClass, Type type, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> stringStringMultivaluedMap, InputStream entityStream)
throws IOException, WebApplicationException {
InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8");
try {
try (InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8")) {
return getGson().fromJson(reader, type);
} finally {
reader.close();
}
}
@ -84,11 +79,8 @@ public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, Messag
MultivaluedMap<String, Object> stringObjectMultivaluedMap, OutputStream entityStream)
throws IOException, WebApplicationException {
OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8);
try {
try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8)) {
getGson().toJson(object, type, writer);
} finally {
writer.close();
}
}
}

@ -28,22 +28,19 @@ import java.util.List;
@ApiModel(description = "Error Response")
public class ErrorResponse {
private Long code = null;
private Integer code = null;
private String message = null;
private String description = null;
private String moreInfo = null;
private List<ErrorListItem> errorItems = new ArrayList<>();
private ErrorResponse() {
}
@JsonProperty(value = "code")
@ApiModelProperty(required = true, value = "")
public Long getCode() {
public Integer getCode() {
return code;
}
public void setCode(Long code) {
public void setCode(Integer code) {
this.code = code;
}
@ -94,57 +91,4 @@ public class ErrorResponse {
public void setErrorItems(List<ErrorListItem> error) {
this.errorItems = error;
}
public static class ErrorResponseBuilder {
private Long code = null;
private String message = null;
private String description = null;
private String moreInfo = null;
private List<ErrorListItem> error;
public ErrorResponseBuilder() {
this.error = new ArrayList<>();
}
public ErrorResponseBuilder setCode(long code) {
this.code = code;
return this;
}
public ErrorResponseBuilder setMessage(String message) {
this.message = message;
return this;
}
public ErrorResponseBuilder setDescription(String description) {
this.description = description;
return this;
}
public ErrorResponseBuilder setMoreInfo(String moreInfo) {
this.moreInfo = moreInfo;
return this;
}
public ErrorResponseBuilder addErrorItem(String code, String msg) {
ErrorListItem item = new ErrorListItem();
item.setCode(code);
item.setMessage(msg);
this.error.add(item);
return this;
}
public ErrorResponse build() {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setCode(code);
errorResponse.setMessage(message);
errorResponse.setErrorItems(error);
errorResponse.setDescription(description);
errorResponse.setMoreInfo(moreInfo);
return errorResponse;
}
}
}

@ -1,59 +0,0 @@
/*
* Copyright (c) 2016, 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.common;
/**
* Custom exception class for handling Application-Mgt API related exceptions.
*/
public class ApplicationMgtAPIException extends Exception {
private static final long serialVersionUID = 7950151650447893900L;
private String errorMessage;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public ApplicationMgtAPIException(String msg, Exception e) {
super(msg, e);
setErrorMessage(msg);
}
public ApplicationMgtAPIException(String msg, Throwable cause) {
super(msg, cause);
setErrorMessage(msg);
}
public ApplicationMgtAPIException(String msg) {
super(msg);
setErrorMessage(msg);
}
public ApplicationMgtAPIException() {
super();
}
public ApplicationMgtAPIException(Throwable cause) {
super(cause);
}
}

@ -1,33 +0,0 @@
/*
* Copyright (c) 2016, 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.common;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
@Produces({ "application/json", "application/xml" })
public class ErrorHandler implements ExceptionMapper<ApplicationMgtAPIException> {
public Response toResponse(ApplicationMgtAPIException exception) {
ErrorMessage errorMessage = new ErrorMessage();
errorMessage.setErrorMessage(exception.getErrorMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}
}

@ -1,42 +0,0 @@
/*
* Copyright (c) 2016, 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.common;
public class ErrorMessage {
private String errorMessage;
private String errorCode;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
}

@ -76,14 +76,14 @@ public interface ApplicationManagementAPI {
@ApiParam(
name = "offset",
value = "Provide how many apps it should return",
value = "Provide from which position apps should return",
required = false,
defaultValue = "20")
@QueryParam("offset") int offset,
@ApiParam(
name = "limit",
value = "Provide from which position apps should return",
value = "Provide how many apps it should return",
required = false,
defaultValue = "0")
@QueryParam("limit") int limit

@ -20,11 +20,11 @@ package org.wso2.carbon.device.application.mgt.api.services.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.api.util.ApplicationMgtAPIUtil;
import org.wso2.carbon.device.application.mgt.api.APIUtil;
import org.wso2.carbon.device.application.mgt.core.services.impl.ApplicationManagementServiceFactory;
import org.wso2.carbon.device.application.mgt.extensions.appupload.AppUploadManager;
@ -50,19 +50,13 @@ public class ApplicationManagementAPIImpl {
@Path("applications")
public Response getApplications(@QueryParam("offset") int offset, @QueryParam("limit") int limit,
@QueryParam("q") String searchQuery) {
ApplicationManagementServiceFactory serviceFactory = ApplicationMgtAPIUtil.getApplicationManagementServiceFactory();
ApplicationManagementServiceFactory serviceFactory = APIUtil.getApplicationManagementServiceFactory();
ApplicationManager applicationManager = (ApplicationManager) serviceFactory
.getApplicationManagementService(APPLICATION_MANAGER);
AppUploadManager appUploadManager = (AppUploadManager) serviceFactory
.applicationManagementExtensionsService(APPLICATION_UPLOAD_EXTENSION);
try {
if (limit == 0) {
limit = DEFAULT_LIMIT;
}
Filter filter = new Filter();
filter.setOffset(offset);
filter.setLimit(limit);
@ -70,7 +64,7 @@ public class ApplicationManagementAPIImpl {
ApplicationList applications = applicationManager.getApplications(filter);
return Response.status(Response.Status.OK).entity(applications).build();
} catch (Exception e) {
} catch (ApplicationManagementException e) {
String msg = "Error occurred while getting the application list";
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).build();

@ -1,44 +0,0 @@
/*
* 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.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.core.services.impl.ApplicationManagementServiceFactory;
/**
* Holds util methods required for Application-Mgt API component.
*/
public class ApplicationMgtAPIUtil {
private static Log log = LogFactory.getLog(ApplicationMgtAPIUtil.class);
public static ApplicationManagementServiceFactory getApplicationManagementServiceFactory() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ApplicationManagementServiceFactory applicationManagerServiceFactory =
(ApplicationManagementServiceFactory) ctx.getOSGiService(ApplicationManagementServiceFactory.class, null);
if (applicationManagerServiceFactory == null) {
String msg = "Application Management provider service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
return applicationManagerServiceFactory;
}
}

@ -33,6 +33,6 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
</jaxrs:server>
<bean id="applicationMgtServiceBean" class="org.wso2.carbon.device.application.mgt.api.services.impl.ApplicationManagementAPIImpl"/>
<bean id="jsonProvider" class="org.wso2.carbon.device.application.mgt.api.common.GsonMessageBodyHandler"/>
<bean id="jsonProvider" class="org.wso2.carbon.device.application.mgt.api.JSONMessageHandler"/>
</beans>

@ -54,7 +54,7 @@
<filter>
<filter-name>ApiOriginFilter</filter-name>
<filter-class>org.wso2.carbon.device.application.mgt.api.ApiOriginFilter</filter-class>
<filter-class>org.wso2.carbon.device.application.mgt.api.APIOriginFilter</filter-class>
</filter>
<filter>

@ -18,15 +18,20 @@
*/
package org.wso2.carbon.device.application.mgt.common.exception;
public class ApplicationManagerException extends Exception {
public abstract class ApplicationManagementException extends Exception {
String message;
public ApplicationManagerException(String message, Throwable throwable){
public ApplicationManagementException(String message, Throwable throwable){
super(message, throwable);
setMessage(message);
}
public ApplicationManagementException(String message){
super(message);
setMessage(message);
}
@Override
public String getMessage() {
return message;

@ -17,14 +17,10 @@
*/
package org.wso2.carbon.device.application.mgt.common.exception;
public class DBConnectionException extends Exception {
public class DBConnectionException extends ApplicationManagementException {
private static final long serialVersionUID = -3151279331929070297L;
public DBConnectionException(String msg, Exception nestedEx) {
super(msg, nestedEx);
}
public DBConnectionException(String message, Throwable cause) {
super(message, cause);
}
@ -32,13 +28,4 @@ public class DBConnectionException extends Exception {
public DBConnectionException(String msg) {
super(msg);
}
public DBConnectionException() {
super();
}
public DBConnectionException(Throwable cause) {
super(cause);
}
}

@ -17,7 +17,7 @@
*/
package org.wso2.carbon.device.application.mgt.common.exception;
public class IllegalTransactionStateException extends RuntimeException {
public class IllegalTransactionStateException extends ApplicationManagementException {
private static final long serialVersionUID = -3151279331929070297L;
@ -33,12 +33,4 @@ public class IllegalTransactionStateException extends RuntimeException {
super(msg);
}
public IllegalTransactionStateException() {
super();
}
public IllegalTransactionStateException(Throwable cause) {
super(cause);
}
}

@ -17,7 +17,7 @@
*/
package org.wso2.carbon.device.application.mgt.common.exception;
public class TransactionManagementException extends Exception {
public class TransactionManagementException extends ApplicationManagementException {
private static final long serialVersionUID = -3151279321929070297L;
@ -33,12 +33,4 @@ public class TransactionManagementException extends Exception {
super(msg);
}
public TransactionManagementException() {
super();
}
public TransactionManagementException(Throwable cause) {
super(cause);
}
}

@ -21,7 +21,7 @@ package org.wso2.carbon.device.application.mgt.common.exception;
/**
* This runtime exception will be thrown if the server has configured with unsupported DB engine.
*/
public class UnsupportedDatabaseEngineException extends RuntimeException {
public class UnsupportedDatabaseEngineException extends ApplicationManagementException {
private static final long serialVersionUID = -3151279311929070297L;
@ -37,12 +37,4 @@ public class UnsupportedDatabaseEngineException extends RuntimeException {
super(msg);
}
public UnsupportedDatabaseEngineException() {
super();
}
public UnsupportedDatabaseEngineException(Throwable cause) {
super(cause);
}
}

@ -21,11 +21,11 @@ package org.wso2.carbon.device.application.mgt.common.services;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagerException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
public interface ApplicationManager extends ApplicationManagementService {
void createApplication(Application application) throws ApplicationManagerException;
void createApplication(Application application) throws ApplicationManagementException;
ApplicationList getApplications(Filter filter) throws ApplicationManagerException;
ApplicationList getApplications(Filter filter) throws ApplicationManagementException;
}

@ -21,7 +21,7 @@ package org.wso2.carbon.device.application.mgt.core.config;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagerException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil;
import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagerConstants;
import org.wso2.carbon.utils.CarbonUtils;
@ -51,7 +51,7 @@ public class ApplicationConfigurationManager {
applicationConfigurationManager = new ApplicationConfigurationManager();
try {
applicationConfigurationManager.initConfig();
} catch (ApplicationManagerException e) {
} catch (ApplicationManagementException e) {
log.error(e);
}
}
@ -60,7 +60,7 @@ public class ApplicationConfigurationManager {
}
public synchronized void initConfig() throws ApplicationManagerException {
public synchronized void initConfig() throws ApplicationManagementException {
try {
File appMgtConfig = new File(applicationMgtConfigXMLPath);
Document doc = ApplicationManagementUtil.convertToDocument(appMgtConfig);
@ -71,7 +71,7 @@ public class ApplicationConfigurationManager {
this.applicationManagerConfiguration = (ApplicationManagementConfigurations) unmarshaller.unmarshal(doc);
} catch (Exception e) {
log.error(e);
throw new ApplicationManagerException("Error occurred while initializing application config", e);
throw new ApplicationManagementException("Error occurred while initializing application config", e);
}
}

@ -18,9 +18,9 @@
*/
package org.wso2.carbon.device.application.mgt.core.dao.common;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagerException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
public class ApplicationManagementDAOException extends ApplicationManagerException {
public class ApplicationManagementDAOException extends ApplicationManagementException {
public ApplicationManagementDAOException(String message, Throwable throwable) {
super(message, throwable);

@ -21,7 +21,7 @@ package org.wso2.carbon.device.application.mgt.core.services.impl;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagerException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO;
@ -31,21 +31,21 @@ import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
public class ApplicationManagerImpl implements ApplicationManager {
@Override
public void createApplication(Application application) throws ApplicationManagerException {
public void createApplication(Application application) throws ApplicationManagementException {
}
@Override
public ApplicationList getApplications(Filter filter) throws ApplicationManagerException {
public ApplicationList getApplications(Filter filter) throws ApplicationManagementException {
try {
ConnectionManagerUtil.openConnection();
ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
return applicationDAO.getApplications(filter);
} catch (ApplicationManagementDAOException e) {
throw new ApplicationManagerException("Error occurred while obtaining the applications for " +
throw new ApplicationManagementException("Error occurred while obtaining the applications for " +
"the given filter.", e);
} catch (DBConnectionException e) {
throw new ApplicationManagerException("Error occurred while opening a connection to the APPM data source", e);
throw new ApplicationManagementException("Error occurred while opening a connection to the APPM data source", e);
} finally {
ConnectionManagerUtil.closeConnection();
}

@ -21,9 +21,8 @@ package org.wso2.carbon.device.application.mgt.core.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagerException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.core.services.impl.ApplicationManagementServiceFactory;
import javax.xml.XMLConstants;
@ -47,7 +46,7 @@ public class ApplicationManagementUtil {
return applicationManagerServiceFactory;
}
public static Document convertToDocument(File file) throws ApplicationManagerException {
public static Document convertToDocument(File file) throws ApplicationManagementException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
@ -55,7 +54,7 @@ public class ApplicationManagementUtil {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
return docBuilder.parse(file);
} catch (Exception e) {
throw new ApplicationManagerException("Error occurred while parsing file, while converting " +
throw new ApplicationManagementException("Error occurred while parsing file, while converting " +
"to a org.w3c.dom.Document : ", e);
}
}

Loading…
Cancel
Save