wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r812683 - in /incubator/wink/trunk: wink-client/src/main/java/org/apache/wink/client/ wink-client/src/main/java/org/apache/wink/client/internal/ wink-client/src/main/java/org/apache/wink/client/internal/handlers/ wink-common/src/main/java/o...
Date Tue, 08 Sep 2009 20:40:24 GMT
Author: bluk
Date: Tue Sep  8 20:40:23 2009
New Revision: 812683

URL: http://svn.apache.org/viewvc?rev=812683&view=rev
Log:
Add AcceptHeaderHandler to client.

Move the setting of the client Accept header
Allows handlers to set the header if necessary.

Add AcceptHeaderHandler to automatically set
Accept header if possible.

Also add option to disable handler.

See [WINK-162]

Added:
    incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AcceptHeaderHandler.java
  (with props)
Modified:
    incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/ClientConfig.java
    incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/ResourceImpl.java
    incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java
    incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties

Modified: incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/ClientConfig.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/ClientConfig.java?rev=812683&r1=812682&r2=812683&view=diff
==============================================================================
--- incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/ClientConfig.java
(original)
+++ incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/ClientConfig.java
Tue Sep  8 20:40:23 2009
@@ -22,7 +22,6 @@
 
 import java.io.FileNotFoundException;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
@@ -31,6 +30,7 @@
 
 import org.apache.wink.client.handlers.ClientHandler;
 import org.apache.wink.client.handlers.ConnectionHandler;
+import org.apache.wink.client.internal.handlers.AcceptHeaderHandler;
 import org.apache.wink.client.internal.handlers.HttpURLConnectionHandler;
 import org.apache.wink.common.WinkApplication;
 import org.apache.wink.common.internal.application.ApplicationFileLoader;
@@ -51,6 +51,7 @@
     private LinkedList<ClientHandler> handlers;
     private LinkedList<Application>   applications;
     private boolean                   modifiable;
+    private boolean                   isAcceptHeaderAutoSet;
 
     /**
      * Construct a new ClientConfig with the following default settings:
@@ -68,6 +69,7 @@
         connectTimeout = 60000;
         readTimeout = 60000;
         followRedirects = true;
+        isAcceptHeaderAutoSet = true;
         handlers = new LinkedList<ClientHandler>();
         applications = new LinkedList<Application>();
         initDefaultApplication();
@@ -220,6 +222,33 @@
     }
 
     /**
+     * Returns whether client will automatically set an appropriate Accept
+     * header
+     * 
+     * @return true if client will automatically set an appropriate Accept
+     *         header; false otherwise
+     */
+    public final boolean isAcceptHeaderAutoSet() {
+        return isAcceptHeaderAutoSet;
+    }
+
+    /**
+     * Set whether client will automatically set an appropriate Accept header
+     * 
+     * @param isAcceptHeaderAutoSet whether client will automatically set an
+     *            appropriate Accept header
+     * @return this client configuration
+     * @throws ClientConfigException
+     */
+    public final ClientConfig acceptHeaderAutoSet(boolean isAcceptHeaderAutoSet) {
+        if (!modifiable) {
+            throw new ClientConfigException("configuration is unmodifiable");
+        }
+        this.isAcceptHeaderAutoSet = isAcceptHeaderAutoSet;
+        return this;
+    }
+
+    /**
      * Get an unmodifiable list of the client handlers
      * 
      * @return an unmodifiable list of the client handlers
@@ -246,6 +275,9 @@
     }
 
     /* package */ClientConfig build() {
+        if (isAcceptHeaderAutoSet) {
+            handlers.add(new AcceptHeaderHandler());
+        }
         handlers.add(getConnectionHandler());
         modifiable = false;
         return this;

Modified: incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/ResourceImpl.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/ResourceImpl.java?rev=812683&r1=812682&r2=812683&view=diff
==============================================================================
--- incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/ResourceImpl.java
(original)
+++ incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/ResourceImpl.java
Tue Sep  8 20:40:23 2009
@@ -222,9 +222,6 @@
         request.setURI(uriBuilder.build());
         request.setMethod(method);
         request.getHeaders().putAll(headers);
-        if (headers.getFirst(HttpHeaders.ACCEPT) == null) {
-            request.getHeaders().add(HttpHeaders.ACCEPT, MediaType.WILDCARD);
-        }
         if (headers.getFirst(HttpHeaders.USER_AGENT) == null) {
             request.getHeaders().add(HttpHeaders.USER_AGENT, USER_AGENT);
         }

Added: incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AcceptHeaderHandler.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AcceptHeaderHandler.java?rev=812683&view=auto
==============================================================================
--- incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AcceptHeaderHandler.java
(added)
+++ incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AcceptHeaderHandler.java
Tue Sep  8 20:40:23 2009
@@ -0,0 +1,78 @@
+/*
+ * 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.internal.handlers;
+
+import java.util.Set;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+
+import org.apache.wink.client.ClientRequest;
+import org.apache.wink.client.ClientResponse;
+import org.apache.wink.client.handlers.ClientHandler;
+import org.apache.wink.client.handlers.HandlerContext;
+import org.apache.wink.common.internal.i18n.Messages;
+import org.apache.wink.common.internal.registry.ProvidersRegistry;
+import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provides support for setting the Accept header automatically.
+ */
+public class AcceptHeaderHandler implements ClientHandler {
+
+    private static final Logger logger = LoggerFactory.getLogger(AcceptHeaderHandler.class);
+
+    public ClientResponse handle(ClientRequest request, HandlerContext context) throws Exception
{
+        MultivaluedMap<String, String> requestHeaders = request.getHeaders();
+        if (requestHeaders.getFirst(HttpHeaders.ACCEPT) == null) {
+            Object responseEntityClassType =
+                request.getAttributes().get(ClientRequestImpl.RESPONSE_ENTITY_CLASS_TYPE);
+            if (responseEntityClassType != null) {
+                Class<?> classType = (Class<?>)responseEntityClassType;
+                logger.debug("Response entity class is: {}", classType);
+                Set<MediaType> mediaTypes =
+                    request.getAttribute(ProvidersRegistry.class)
+                        .getMessageBodyReaderMediaTypesLimitByIsReadable(classType,
+                                                                         RuntimeContextTLS
+                                                                             .getRuntimeContext());
+                logger.debug("Found media types: {}", mediaTypes);
+                StringBuffer acceptHeader = new StringBuffer();
+                boolean isFirst = true;
+                for (MediaType mt : mediaTypes) {
+                    if (!isFirst) {
+                        acceptHeader.append(",");
+                    }
+                    acceptHeader.append(mt.toString());
+                    isFirst = false;
+                }
+                if (acceptHeader.length() > 0) {
+                    String acceptValue = acceptHeader.toString();
+                    requestHeaders.add(HttpHeaders.ACCEPT, acceptValue);
+                    logger.info(Messages.getMessage("clientAcceptHeaderHandlerSetAccept"),
+                                acceptValue);
+                }
+            }
+        }
+        return context.doChain(request);
+    }
+}

Propchange: incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/AcceptHeaderHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java?rev=812683&r1=812682&r2=812683&view=diff
==============================================================================
--- incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java
(original)
+++ incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/internal/handlers/HttpURLConnectionHandler.java
Tue Sep  8 20:40:23 2009
@@ -29,6 +29,8 @@
 import java.net.URL;
 import java.util.List;
 
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.wink.client.ClientConfig;
@@ -101,6 +103,14 @@
                 }
             }
         }
+        /*
+         * HttpUrlConnection may set an illegal Accept header by default (a
+         * "*;q=0.2" without a subtytle) so if there wasn't an Accept header,
+         * then set one here.
+         */
+        if (headers.getFirst(HttpHeaders.ACCEPT) == null) {
+            connection.addRequestProperty(HttpHeaders.ACCEPT, MediaType.WILDCARD);
+        }
     }
 
     private ClientResponse processResponse(ClientRequest request,

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=812683&r1=812682&r2=812683&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
Tue Sep  8 20:40:23 2009
@@ -347,6 +347,37 @@
         }
     }
 
+    public Set<MediaType> getMessageBodyReaderMediaTypesLimitByIsReadable(Class<?>
type,
+                                                                          RuntimeContext
runtimeContext) {
+        Set<MediaType> mediaTypes = new HashSet<MediaType>();
+        readersLock.lock();
+        try {
+            List<ObjectFactory<MessageBodyReader<?>>> readerFactories =
+                messageBodyReaders.getProvidersByMediaType(MediaType.WILDCARD_TYPE, type);
+
+            Annotation[] ann = new Annotation[0];
+            for (ObjectFactory<MessageBodyReader<?>> factory : readerFactories)
{
+                MessageBodyReader<?> reader = factory.getInstance(runtimeContext);
+                Consumes consumes = factory.getInstanceClass().getAnnotation(Consumes.class);
+                String[] values = null;
+                if (consumes != null) {
+                    values = consumes.value();
+                } else {
+                    values = new String[] {MediaType.WILDCARD};
+                }
+                for (String v : values) {
+                    MediaType mt = MediaType.valueOf(v);
+                    if (reader.isReadable(type, type, ann, mt)) {
+                        mediaTypes.add(mt);
+                    }
+                }
+            }
+        } finally {
+            readersLock.unlock();
+        }
+        return mediaTypes;
+    }
+
     public Set<MediaType> getMessageBodyWriterMediaTypes(Class<?> type) {
         if (type == null) {
             throw new NullPointerException("type");

Modified: incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties?rev=812683&r1=812682&r2=812683&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
(original)
+++ incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
Tue Sep  8 20:40:23 2009
@@ -110,4 +110,7 @@
 # WebDAV
 webDAVNoEditOrSelfLink=The resource {} has set no edit or self link
 
-noMethodInClassSupportsHTTPMethod=Could not find any method in class {} that supports {}
\ No newline at end of file
+noMethodInClassSupportsHTTPMethod=Could not find any method in class {} that supports {}
+
+# Client Handler
+clientAcceptHeaderHandlerSetAccept=Accept header automatically set to: {}
\ No newline at end of file



Mime
View raw message