Repository: incubator-stratos Updated Branches: refs/heads/master 163f0fe0f -> b52875671
fixing STRATOS-466 Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/7f9950c7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/7f9950c7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/7f9950c7 Branch: refs/heads/master Commit: 7f9950c7eb535b00889c3af6a6709504f857b446 Parents: 9c0878b Author: Pradeep Fernando <[email protected]> Authored: Fri Apr 18 12:12:15 2014 +0530 Committer: Pradeep Fernando <[email protected]> Committed: Fri Apr 18 12:12:15 2014 +0530 ---------------------------------------------------------------------- .../handlers/BadRequestExceptionMapper.java | 45 +++++++++++++++++++ .../handlers/CustomExceptionMapper.java | 13 +++++- .../handlers/GenericExceptionMapper.java | 46 ++++++++++++++++++++ .../main/webapp/stratos/WEB-INF/cxf-servlet.xml | 15 +++++++ .../webapp/stratos/WEB-INF/schemas/schema.xsd | 21 +++++++++ 5 files changed, 139 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/7f9950c7/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/BadRequestExceptionMapper.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/BadRequestExceptionMapper.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/BadRequestExceptionMapper.java new file mode 100644 index 0000000..2fcc638 --- /dev/null +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/BadRequestExceptionMapper.java @@ -0,0 +1,45 @@ +package org.apache.stratos.rest.endpoint.handlers;/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + */ + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.stratos.rest.endpoint.Utils; + +import javax.ws.rs.BadRequestException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +/* +*Converts the badRequestException errors to appropriate json output messages. Introduced to +* converts the JAXBExceptions due to wrong input formats +**/ +public class BadRequestExceptionMapper implements ExceptionMapper<BadRequestException> { + private static Log log = LogFactory.getLog(BadRequestExceptionMapper.class); + + public Response toResponse(BadRequestException badRequestException) { + if(log.isDebugEnabled()){ + log.debug("Error in input format", badRequestException); + } + String errorMsg = badRequestException.getMessage() != null ? badRequestException.getMessage() : "please check" + + "your input format"; + return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON). + entity(Utils.buildMessage(Response.Status.BAD_REQUEST.getStatusCode(),errorMsg )).build(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/7f9950c7/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/CustomExceptionMapper.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/CustomExceptionMapper.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/CustomExceptionMapper.java index 3fe6ea4..3a5bc3d 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/CustomExceptionMapper.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/CustomExceptionMapper.java @@ -16,6 +16,8 @@ package org.apache.stratos.rest.endpoint.handlers;/* * under the License. */ +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.stratos.rest.endpoint.Utils; import org.apache.stratos.rest.endpoint.exception.RestAPIException; @@ -23,15 +25,24 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; +/* +* Stratos admin APIs' throw {@link RestAPIException} upon failure. This Class +* maps such exceptions to appropriate JSON output +* */ public class CustomExceptionMapper implements ExceptionMapper<RestAPIException> { + private static Log log = LogFactory.getLog(CustomExceptionMapper.class); + public Response toResponse(RestAPIException restAPIException) { + if(log.isDebugEnabled()){ + log.debug("Error while invoking the admin rest api", restAPIException); + } // if no specific error message specified, spitting out a generaic error message String errorMessage = (restAPIException.getMessage() != null)? restAPIException.getMessage():"Error while fullfilling the request"; // if no specific error specified we are throwing the bad request http status code by default Response.Status httpStatus= (restAPIException.getHTTPStatusCode() != null)? restAPIException.getHTTPStatusCode():Response.Status.BAD_REQUEST; - return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON). + return Response.status(httpStatus.getStatusCode()).type(MediaType.APPLICATION_JSON). entity(Utils.buildMessage(httpStatus.getStatusCode(),errorMessage)).build(); } } http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/7f9950c7/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/GenericExceptionMapper.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/GenericExceptionMapper.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/GenericExceptionMapper.java new file mode 100644 index 0000000..65d2ed1 --- /dev/null +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/handlers/GenericExceptionMapper.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.apache.stratos.rest.endpoint.handlers; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.stratos.rest.endpoint.Utils; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +/*This class maps any exception thrown by the server, which is not mapped by a specifi exception mapper +* in to an appropriate format +* */ +public class GenericExceptionMapper implements ExceptionMapper<WebApplicationException> { + private static Log log = LogFactory.getLog(GenericExceptionMapper.class); + + public Response toResponse(WebApplicationException webApplicationException) { + if(log.isDebugEnabled()){ + log.debug("internal server error", webApplicationException); + } + // if no specific error message specified, spitting out a generaic error message + String errorMessage = (webApplicationException.getMessage() != null)? + webApplicationException.getMessage():"internal server error"; + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON). + entity(Utils.buildMessage(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), errorMessage)).build(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/7f9950c7/components/org.apache.stratos.rest.endpoint/src/main/webapp/stratos/WEB-INF/cxf-servlet.xml ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/webapp/stratos/WEB-INF/cxf-servlet.xml b/components/org.apache.stratos.rest.endpoint/src/main/webapp/stratos/WEB-INF/cxf-servlet.xml index fbb8b10..624cb24 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/webapp/stratos/WEB-INF/cxf-servlet.xml +++ b/components/org.apache.stratos.rest.endpoint/src/main/webapp/stratos/WEB-INF/cxf-servlet.xml @@ -21,8 +21,10 @@ <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:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <jaxrs:server id="stratosAdmin" address="/"> @@ -31,6 +33,8 @@ </jaxrs:serviceBeans> <jaxrs:providers> + <ref bean="genericExceptionHandler"/> + <ref bean="badRequestExceptionHandler"/> <ref bean="jsonProvider"/> <ref bean="exceptionHandler"/> <ref bean="basicAuthenticationFilter"/> @@ -46,6 +50,8 @@ <property name="securedObject" ref="stratosRestEndpointBean"/> </bean> <bean id="exceptionHandler" class="org.apache.stratos.rest.endpoint.handlers.CustomExceptionMapper"/> + <bean id="badRequestExceptionHandler" class="org.apache.stratos.rest.endpoint.handlers.BadRequestExceptionMapper"/> + <bean id="genericExceptionHandler" class="org.apache.stratos.rest.endpoint.handlers.GenericExceptionMapper"/> <!--The below config enables OAuth based authentication/authorization for REST API--> <bean id="OAuthFilter" class="org.apache.stratos.rest.endpoint.handlers.OAuthHandler"> <property name="password" value="admin"/> @@ -53,6 +59,7 @@ <property name="oauthValidationEndpoint" value="https://localhost:9443/services/"/> </bean> <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider"> + <property name="schemaHandler" ref="schemaHolder"/> <property name="supportUnwrapped" value="true"/> <property name="serializeAsArray" value="true"/> <property name="arrayKeys"> @@ -72,4 +79,12 @@ </property> </bean> + <bean id="schemaHolder" class="org.apache.cxf.jaxrs.utils.schemas.SchemaHandler"> + <property name="schemas" ref="theSchemas"/> + </bean> + + <util:list id="theSchemas"> + <value>classpath:/WEB-INF/schemas/schema.xsd</value> + </util:list> + </beans> http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/7f9950c7/components/org.apache.stratos.rest.endpoint/src/main/webapp/stratos/WEB-INF/schemas/schema.xsd ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/webapp/stratos/WEB-INF/schemas/schema.xsd b/components/org.apache.stratos.rest.endpoint/src/main/webapp/stratos/WEB-INF/schemas/schema.xsd new file mode 100644 index 0000000..b003664 --- /dev/null +++ b/components/org.apache.stratos.rest.endpoint/src/main/webapp/stratos/WEB-INF/schemas/schema.xsd @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> + <xs:element name="partition"> + <xs:complexType> + <xs:sequence> + <xs:element name="id" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/> + <xs:element name="provider" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/> + <xs:element name="property" minOccurs="1" maxOccurs="unbounded"> + <xs:complexType> + <xs:sequence> + <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/> + <xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/> + </xs:sequence> + </xs:complexType> + </xs:element> + <xs:element name="partitionMin" type="xs:int" minOccurs="1" maxOccurs="1" nillable="false"/> + <xs:element name="partitionMax" type="xs:int" minOccurs="1" maxOccurs="1" nillable="false"/> + </xs:sequence> + </xs:complexType> + </xs:element> +</xs:schema> \ No newline at end of file
