/*
 * 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.trace("Response entity class is: {}", classType); //$NON-NLS-1$
                Set<MediaType> mediaTypes =
                    request.getAttribute(ProvidersRegistry.class)
                        .getMessageBodyReaderMediaTypesLimitByIsReadable(classType,
                                                                         RuntimeContextTLS
                                                                             .getRuntimeContext());
                logger.trace("Found media types: {}", mediaTypes); //$NON-NLS-1$
                StringBuffer acceptHeader = new StringBuffer();
                boolean isFirst = true;
                for (MediaType mt : mediaTypes) {
                    if (!isFirst) {
                        acceptHeader.append(","); //$NON-NLS-1$
                    }
                    acceptHeader.append(mt.toString());
                    isFirst = false;
                }
                if (acceptHeader.length() > 0) {
                    String acceptValue = acceptHeader.toString();
                    requestHeaders.add(HttpHeaders.ACCEPT, acceptValue);
                    logger.info(Messages.getMessage("clientAcceptHeaderHandlerSetAccept", //$NON-NLS-1$
                                acceptValue));
                }
            }
        }
        return context.doChain(request);
    }
}

@@ -2171,16 +2171,93 @@
 ssType;%0A
+                if (!classType.isAssignableFrom(ClientResponse.class)) %7B%0A    
         
@@ -2351,16 +2351,20 @@
         
+    
 Set%3CMedi
@@ -2395,39 +2395,72 @@
                 
+    
 request
+%0A                            
 .getAttribute(Pr
@@ -2502,24 +2502,28 @@
             
+    
 .getMessageB
@@ -2570,16 +2570,20 @@
 ssType,%0A
+    
         
@@ -2738,24 +2738,28 @@
             
+    
 .getRuntimeC
@@ -2777,32 +2777,36 @@
                 
+   
+ 
 logger.trace(%22Fo
@@ -2866,24 +2866,28 @@
             
+    
 StringBuffer
@@ -2938,16 +2938,20 @@
         
+  
+  
 boolean 
@@ -2966,16 +2966,20 @@
 = true;%0A
+    
         
@@ -3036,24 +3036,28 @@
             
+ 
+   
 if (!isFirst
@@ -3056,24 +3056,28 @@
 !isFirst) %7B%0A
+    
             
@@ -3140,34 +3140,41 @@
                 
-%7D%0A
+    %7D%0A   
                 
@@ -3169,32 +3169,33 @@
                 
+ 
 acceptHeader.app
@@ -3234,16 +3234,20 @@
         
+    
 isFirst 
@@ -3263,34 +3263,42 @@
                 
+  
-%7D%0A
+  %7D%0A    
                 
@@ -3346,24 +3346,28 @@
             
+ 
+   
 String accep
@@ -3396,24 +3396,28 @@
 toString();%0A
+    
             
@@ -3493,24 +3493,28 @@
             
+ 
+   
 logger.info(
@@ -3613,24 +3613,48 @@
             
+                  
+      
 acceptValue)
@@ -3652,24 +3652,46 @@
 eptValue));%0A
+                    %7D%0A
             

Fix issue where json media types were being used by default for the accept header when the user did not specify one and a ClientResponse is being returned by the RestClient