diff options
Diffstat (limited to '')
5 files changed, 57 insertions, 23 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSProvider.java index d7eeede240..1ccdb868d7 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSProvider.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSProvider.java @@ -90,8 +90,10 @@ public abstract class DataBindingJAXRSProvider { } protected boolean supports(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { - return MediaType.APPLICATION_JSON_TYPE.equals(mediaType) || MediaType.APPLICATION_XML_TYPE.equals(mediaType) - || MediaType.TEXT_XML_TYPE.equals(mediaType); + // Some media types have parameters + mediaType = new MediaType(mediaType.getType(), mediaType.getSubtype()); + return MediaType.APPLICATION_JSON_TYPE.isCompatible(mediaType) || MediaType.APPLICATION_XML_TYPE.isCompatible(mediaType) + || MediaType.TEXT_XML_TYPE.isCompatible(mediaType); } protected Object convert(InputStream content, String contentType, Class<?> type) throws IOException { diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSReader.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSReader.java index 035af0aedf..d6df778feb 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSReader.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSReader.java @@ -62,6 +62,7 @@ public class DataBindingJAXRSReader<T> extends DataBindingJAXRSProvider implemen String dataBinding = null; + mediaType = new MediaType(mediaType.getType(), mediaType.getSubtype()); // FIXME: [rfeng] This is a hack to handle application/json if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) { dataBinding = mediaType.toString() + "#" + InputStream.class.getName(); diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSWriter.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSWriter.java index 8503d089eb..740b685b22 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSWriter.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/DataBindingJAXRSWriter.java @@ -62,7 +62,7 @@ public class DataBindingJAXRSWriter<T> extends DataBindingJAXRSProvider implemen MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { DataType dataType = createDataType(type, genericType); - + mediaType = new MediaType(mediaType.getType(), mediaType.getSubtype()); String dataBinding = OutputStream.class.getName(); // FIXME: [rfeng] This is a hack to handle application/json if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) { diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java index fdabb13ce5..b672c3ee2d 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java @@ -221,7 +221,7 @@ public class RESTServiceBindingProvider implements EndpointProvider { if (isJAXRS) { application = new SimpleApplication(interfaze); - TuscanyRESTServlet restServlet = new TuscanyRESTServlet(extensionPoints); + TuscanyRESTServlet restServlet = new TuscanyRESTServlet(extensionPoints, application.resourceClass); // Create our HTTP service listener Servlet and register it with the // Servlet host diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java index 3d561bf21f..13f13e4dde 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java @@ -24,14 +24,15 @@ import java.util.Enumeration; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.HttpMethod; +import javax.ws.rs.core.MediaType; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.wink.common.internal.registry.ProvidersRegistry; +import org.apache.wink.common.internal.registry.metadata.MethodMetadata; import org.apache.wink.server.internal.DeploymentConfiguration; import org.apache.wink.server.internal.RequestProcessor; -import org.apache.wink.server.internal.handlers.ServerMessageContext; +import org.apache.wink.server.internal.registry.ResourceRecord; import org.apache.wink.server.internal.servlet.RestServlet; /** @@ -40,10 +41,13 @@ import org.apache.wink.server.internal.servlet.RestServlet; public class TuscanyRESTServlet extends RestServlet { private static final long serialVersionUID = 89997233133964915L; private ExtensionPointRegistry registry; + private Class<?> resourceClass; + private boolean fixed; - public TuscanyRESTServlet(ExtensionPointRegistry registry) { + public TuscanyRESTServlet(ExtensionPointRegistry registry, Class<?> resourceClass) { super(); this.registry = registry; + this.resourceClass = resourceClass; } @Override @@ -52,19 +56,19 @@ public class TuscanyRESTServlet extends RestServlet { DeploymentConfiguration config = super.getDeploymentConfiguration(); // [rfeng] FIXME: This is a hack to fool Apache wink to not remove the servlet path config.setFilterConfig(new FilterConfig() { - + public ServletContext getServletContext() { return getServletContext(); } - + public Enumeration getInitParameterNames() { return getInitParameterNames(); } - + public String getInitParameter(String arg0) { return getInitParameter(arg0); } - + public String getFilterName() { return getServletName(); } @@ -72,23 +76,50 @@ public class TuscanyRESTServlet extends RestServlet { ProvidersRegistry providers = config.getProvidersRegistry(); providers.addProvider(new DataBindingJAXRSReader(registry), 0.001, true); providers.addProvider(new DataBindingJAXRSWriter(registry), 0.001, true); + return config; } - @Override - public RequestProcessor getRequestProcessor() { - return super.getRequestProcessor(); + private synchronized void fixMediaTypes(DeploymentConfiguration config) { + if (fixed) { + return; + } + // FIXME: A hacky workaround for https://issues.apache.org/jira/browse/TUSCANY-3572 + ResourceRecord record = config.getResourceRegistry().getRecord(resourceClass); + + for (MethodMetadata methodMetadata : record.getMetadata().getResourceMethods()) { + String method = methodMetadata.getHttpMethod(); + if (HttpMethod.GET.equals(method) || HttpMethod.HEAD.equals(method) || HttpMethod.DELETE.equals(method)) { + methodMetadata.addConsumes(MediaType.APPLICATION_OCTET_STREAM_TYPE); + methodMetadata.addConsumes(MediaType.WILDCARD_TYPE); + } + if (HttpMethod.HEAD.equals(method) || HttpMethod.DELETE.equals(method)) { + methodMetadata.addProduces(MediaType.APPLICATION_OCTET_STREAM_TYPE); + methodMetadata.addConsumes(MediaType.WILDCARD_TYPE); + } + } + for (MethodMetadata methodMetadata : record.getMetadata().getSubResourceMethods()) { + String method = methodMetadata.getHttpMethod(); + if (HttpMethod.GET.equals(method) || HttpMethod.HEAD.equals(method) || HttpMethod.DELETE.equals(method)) { + methodMetadata.addConsumes(MediaType.APPLICATION_OCTET_STREAM_TYPE); + methodMetadata.addConsumes(MediaType.WILDCARD_TYPE); + } + if (HttpMethod.HEAD.equals(method) || HttpMethod.DELETE.equals(method)) { + methodMetadata.addProduces(MediaType.APPLICATION_OCTET_STREAM_TYPE); + methodMetadata.addConsumes(MediaType.WILDCARD_TYPE); + } + } + fixed = true; } - public ServerMessageContext createMessageContext(HttpServletRequest request, HttpServletResponse response) { - ServerMessageContext messageContext; - try { - messageContext = new ServerMessageContext(request, response, getDeploymentConfiguration()); - } catch (Exception e) { - throw new IllegalArgumentException(e); + @Override + public RequestProcessor getRequestProcessor() { + RequestProcessor processor = super.getRequestProcessor(); + // The 1st call returns null + if (processor != null) { + fixMediaTypes(processor.getConfiguration()); } - return messageContext; + return processor; } - } |