You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by jr...@apache.org on 2010/04/26 16:05:16 UTC
svn commit: r938053 - in /incubator/wink/trunk:
wink-client/src/main/java/org/apache/wink/client/internal/handlers/
wink-client/src/test/java/org/apache/wink/client/
wink-common/src/main/java/org/apache/wink/common/internal/registry/
Author: jramos
Date: Mon Apr 26 14:05:15 2010
New Revision: 938053
URL: http://svn.apache.org/viewvc?rev=938053&view=rev
Log:
[WINK-238]: Set the content type on the outgoing request automatically if not already set by the user
Added:
incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/EmptyContentTypeTest.java
Modified:
incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AbstractConnectionHandler.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java
Modified: incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AbstractConnectionHandler.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AbstractConnectionHandler.java?rev=938053&r1=938052&r2=938053&view=diff
==============================================================================
--- incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AbstractConnectionHandler.java (original)
+++ incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AbstractConnectionHandler.java Mon Apr 26 14:05:15 2010
@@ -86,12 +86,20 @@ public abstract class AbstractConnection
genericType = genericEntity.getType();
entity = genericEntity.getEntity();
}
+ MessageBodyWriter writer = null;
+ MediaType contentMediaType = null;
String contentType = request.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
if (contentType == null) {
- contentType = MediaType.APPLICATION_OCTET_STREAM;
- }
- MediaType contentMediaType = MediaType.valueOf(contentType);
- MessageBodyWriter writer =
+ // attempt to infer the media type based on the providers available for this type
+ contentMediaType = providersRegistry.getMessageBodyWriterMediaTypeLimitByIsWritable(type, runtimeContext);
+ if(contentMediaType == null) {
+ // default if we still couldn't find it
+ contentType = MediaType.APPLICATION_OCTET_STREAM;
+ }
+ } else
+ contentMediaType = MediaType.valueOf(contentType);
+ request.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentMediaType.toString());
+ writer =
providersRegistry.getMessageBodyWriter(type,
genericType,
null,
Added: incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/EmptyContentTypeTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/EmptyContentTypeTest.java?rev=938053&view=auto
==============================================================================
--- incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/EmptyContentTypeTest.java (added)
+++ incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/EmptyContentTypeTest.java Mon Apr 26 14:05:15 2010
@@ -0,0 +1,104 @@
+/*******************************************************************************
+ * 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.wink.client;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+
+public class EmptyContentTypeTest extends BaseTest {
+
+ @Provider
+ @Produces("integer/integer")
+ public static class MyIntegerProvider implements MessageBodyWriter<Integer> {
+
+ public long getSize(Integer t,
+ Class<?> type,
+ Type genericType,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ return -1;
+ }
+
+ public boolean isWriteable(Class<?> type,
+ Type genericType,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ return type.isAssignableFrom(Integer.class);
+ }
+
+ public void writeTo(Integer t,
+ Class<?> type,
+ Type genericType,
+ Annotation[] annotations,
+ MediaType mediaType,
+ MultivaluedMap<String, Object> httpHeaders,
+ OutputStream entityStream) throws IOException, WebApplicationException {
+ int i = t;
+ for (int k = 0; k < 32; ++k) {
+ entityStream.write(i & 0x0001);
+ i = i >> 1;
+ }
+ }
+
+ }
+
+ private RestClient getRestClient() {
+ return new RestClient(new ClientConfig().applications(new Application() {
+
+ @Override
+ public Set<Object> getSingletons() {
+ return null;
+ }
+
+ @Override
+ public Set<Class<?>> getClasses() {
+ Set<Class<?>> set = new HashSet<Class<?>>();
+ set.add(MyIntegerProvider.class);
+ return set;
+ }
+
+ }));
+ }
+
+ public void testEmptyContentType() throws Exception {
+ server.setMockResponseCode(200);
+ RestClient client = getRestClient();
+ client.resource(serviceURL + "/integer").put(123);
+ byte[] b = server.getRequestContent();
+ String expectedBytes = "11011110000000000000000000000000";
+ String actualBytes = "";
+ for(int i = 0; i < b.length; ++i)
+ actualBytes += b[i];
+ assertEquals(expectedBytes, actualBytes);
+ }
+}
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java?rev=938053&r1=938052&r2=938053&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java Mon Apr 26 14:05:15 2010
@@ -470,6 +470,41 @@ public class ProvidersRegistry {
return mediaTypes;
}
+ public MediaType getMessageBodyWriterMediaTypeLimitByIsWritable(Class<?> type,
+ RuntimeContext runtimeContext) {
+ List<MediaType> mediaTypes = new ArrayList<MediaType>();
+ logger.debug("Searching MessageBodyWriters media types limited by class type {}", type); //$NON-NLS-1$
+
+ List<MediaTypeMap<MessageBodyWriter<?>>.OFHolder<MessageBodyWriter<?>>> writerFactories =
+ messageBodyWriters.getProvidersByMediaType(MediaType.WILDCARD_TYPE, type);
+ logger.debug("Found all MessageBodyWriter ObjectFactories limited by class type {}", //$NON-NLS-1$
+ writerFactories);
+ Annotation[] ann = new Annotation[0];
+ for (ObjectFactory<MessageBodyWriter<?>> factory : writerFactories) {
+ MessageBodyWriter<?> writer = factory.getInstance(runtimeContext);
+ Produces produces = factory.getInstanceClass().getAnnotation(Produces.class);
+ String[] values = null;
+ if (produces != null) {
+ values = AnnotationUtils.parseConsumesProducesValues(produces.value());
+ } else {
+ values = new String[] {MediaType.WILDCARD};
+ }
+ for (String v : values) {
+ MediaType mt = MediaType.valueOf(v);
+ if (logger.isDebugEnabled()) {
+ List<Annotation> anns = (ann == null) ? null : Arrays.asList(ann);
+ logger.debug("Calling {}.isWritable( {}, {}, {}, {} )", new Object[] {writer, //$NON-NLS-1$
+ type, type, anns, mt});
+ }
+ if (writer.isWriteable(type, type, ann, mt)) {
+ logger.debug("Returning media type {}", mt); //$NON-NLS-1$
+ return mt;
+ }
+ }
+ }
+ return null;
+ }
+
public Set<MediaType> getMessageBodyWriterMediaTypes(Class<?> type) {
if (type == null) {
throw new NullPointerException(Messages.getMessage("variableIsNull", "type")); //$NON-NLS-1$ //$NON-NLS-2$