Author: sergeyb Date: Fri Jan 7 15:26:12 2011 New Revision: 1056347 URL: http://svn.apache.org/viewvc?rev=1056347&view=rev Log: [CXF-3234] Minor update to JAXRSOutInterceptor for response Type be correctly determined
Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java (with props) Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java?rev=1056347&r1=1056346&r2=1056347&view=diff ============================================================================== --- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java (original) +++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java Fri Jan 7 15:26:12 2011 @@ -202,22 +202,21 @@ public class JAXRSOutInterceptor extends invoked = ori == null ? null : ori.getAnnotatedMethod() == null ? ori.getMethodToInvoke() : ori.getAnnotatedMethod(); } + Class<?> targetType = getRawResponseClass(responseObj); - Type genericType = - getGenericResponseType(ori == null ? null : invoked, responseObj); + Type genericType = getGenericResponseType(ori == null ? null : invoked, responseObj, targetType); if (genericType instanceof TypeVariable) { genericType = InjectionUtils.getSuperType(ori.getClassResourceInfo().getServiceClass(), (TypeVariable)genericType); } + Annotation[] annotations = invoked != null ? invoked.getAnnotations() : new Annotation[]{}; + MessageBodyWriter writer = null; MediaType responseType = null; for (MediaType type : availableContentTypes) { writer = ProviderFactory.getInstance(message) - .createMessageBodyWriter(targetType, genericType, - invoked != null ? invoked.getAnnotations() : new Annotation[]{}, - type, - message); + .createMessageBodyWriter(targetType, genericType, annotations, type, message); if (writer != null) { responseType = type; @@ -239,8 +238,6 @@ public class JAXRSOutInterceptor extends LOG.fine("Response content type is: " + responseType.toString()); message.put(Message.CONTENT_TYPE, responseType.toString()); - Annotation[] annotations = invoked != null ? invoked.getAnnotations() : new Annotation[]{}; - long size = writer.getSize(entity, targetType, genericType, annotations, responseType); if (size > 0) { LOG.fine("Setting ContentLength to " + size + " as requested by " @@ -409,11 +406,17 @@ public class JAXRSOutInterceptor extends } } - private Type getGenericResponseType(Method invoked, Object targetObject) { + private Type getGenericResponseType(Method invoked, Object targetObject, Class<?> targetType) { if (GenericEntity.class.isAssignableFrom(targetObject.getClass())) { return ((GenericEntity)targetObject).getType(); + } else if (invoked == null || !invoked.getReturnType().isAssignableFrom(targetType)) { + // when a method has been invoked it is still possible that either an ExceptionMapper + // or a ResponseHandler filter overrides a response entity; if it happens then + // the Type is the class of the response object, unless this new entity is assignable + // to invoked.getReturnType(); same applies to the case when a method returns Response + return targetObject.getClass(); } else { - return invoked == null ? targetObject.getClass() : invoked.getGenericReturnType(); + return invoked.getGenericReturnType(); } } Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java?rev=1056347&r1=1056346&r2=1056347&view=diff ============================================================================== --- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java (original) +++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java Fri Jan 7 15:26:12 2011 @@ -259,7 +259,7 @@ public class JAXRSClientServerResourceCr assertNotNull(in); assertEquals("Exception is not mapped correctly", - "Nonexistent method", + "StringTextWriter - Nonexistent method", getStringFromInputStream(in).trim()); } Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java?rev=1056347&view=auto ============================================================================== --- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java (added) +++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java Fri Jan 7 15:26:12 2011 @@ -0,0 +1,50 @@ +/** + * 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.cxf.systest.jaxrs; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; + +import org.apache.cxf.jaxrs.provider.PrimitiveTextProvider; + +public class StringTextWriter extends PrimitiveTextProvider { + @Override + public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) { + return false; + } + + public void writeTo(Object obj, Class<?> type, Type genType, Annotation[] anns, + MediaType mt, MultivaluedMap<String, Object> headers, + OutputStream os) throws IOException { + if (obj instanceof String && type == String.class && type == genType) { + obj = "Nonexistent method".equals(obj) ? "StringTextWriter - " + obj : obj; + super.writeTo(obj, type, genType, anns, mt, headers, os); + return; + } + throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); + + } +} Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml?rev=1056347&r1=1056346&r2=1056347&view=diff ============================================================================== --- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml (original) +++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml Fri Jan 7 15:26:12 2011 @@ -37,6 +37,7 @@ http://cxf.apache.org/schemas/jaxrs.xsd" <bean class="org.apache.cxf.jaxrs.ext.codegen.CodeGeneratorProvider"/> <ref bean="exceptionMapper"/> <ref bean="exceptionMapper2"/> + <ref bean="plainTextProvider"/> </jaxrs:providers> </jaxrs:server> <bean id="bookstore" scope="prototype" class="org.apache.cxf.systest.jaxrs.BookStore"/> @@ -45,4 +46,5 @@ http://cxf.apache.org/schemas/jaxrs.xsd" <property name="toHandle" value="true"/> </bean> <bean id="exceptionMapper2" class="org.apache.cxf.systest.jaxrs.RuntimeExceptionMapper"/> + <bean id="plainTextProvider" class="org.apache.cxf.systest.jaxrs.StringTextWriter"/> </beans>