/*
 * Copyright 2008 Netflix, Inc.
 *
 * Licensed 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 net.oauth.client;

import java.io.IOException;
import java.util.Map;
import net.oauth.OAuth;
import net.oauth.OAuthMessageFromHttp;
import net.oauth.http.HttpMessage;
import net.oauth.http.HttpResponseMessage;

/**
 * An HTTP response, encapsulated as an OAuthMessage.
 * 
 * @author John Kristian
 */
public final class OAuthResponseMessage extends OAuthMessageFromHttp
{

    protected OAuthResponseMessage(HttpResponseMessage http) throws IOException
    {
        super(http);
        for (Map.Entry<String, String> header : http.headers) {
            if ("WWW-Authenticate".equalsIgnoreCase(header.getKey())) {
                decodeWWWAuthenticate(header.getValue());
            }
        }
    }

    private void decodeWWWAuthenticate(String header)
    {
        for (OAuth.Parameter parameter : decodeAuthorization(header)) {
            if (!"realm".equalsIgnoreCase(parameter.getKey())) {
                addParameter(parameter);
            }
        }
    }

    @Override
    protected void completeParameters() throws IOException
    {
        super.completeParameters();
        if (isDecodable(http.getHeader(HttpMessage.CONTENT_TYPE))) {
            String body = readBodyAsString();
            if (body != null) {
                addParameters(OAuth.decodeForm(body.trim()));
            }
        }
    }

    /**
     * Decide whether a message body with the given Content-Type can be decoded
     * as OAuth parameters.
     */
    protected static boolean isDecodable(String contentType)
    {
        if (contentType != null) {
            int sep = contentType.indexOf(';');
            String mimeType = (sep < 0) ? contentType : contentType.substring(0, sep);
            mimeType = mimeType.trim();
            if ("text/html".equalsIgnoreCase(mimeType)) {
                return false;
            }
        }
        return true;
    }

}

@@ -757,44 +757,8 @@
 p;%0D%0A
-import net.oauth.http.HttpMessage;%0D%0A
 impo
@@ -968,18 +968,16 @@
 ttp%0D%0A%7B%0D%0A
-%0D%0A
     prot
@@ -1228,156 +1228,8 @@
     
-        decodeWWWAuthenticate(header.getValue());%0D%0A            %7D%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A    private void decodeWWWAuthenticate(String header)%0D%0A    %7B%0D%0A
     
@@ -1291,22 +1291,39 @@
 n(header
-)) %7B%0D%0A
+.getValue())) %7B%0D%0A      
         
@@ -1322,24 +1322,26 @@
             
+  
 if (!%22realm%22
@@ -1390,32 +1390,40 @@
                 
+        
 addParameter(par
@@ -1428,24 +1428,66 @@
 arameter);%0D%0A
+                    %7D%0D%0A                %7D%0D%0A
             
@@ -1632,82 +1632,8 @@
 );%0D%0A
-        if (isDecodable(http.getHeader(HttpMessage.CONTENT_TYPE))) %7B%0D%0A    
     
@@ -1667,28 +1667,24 @@
 sString();%0D%0A
-    
         if (
@@ -1704,36 +1704,32 @@
  %7B%0D%0A            
-    
 addParameters(OA
@@ -1759,31 +1759,16 @@
 m()));%0D%0A
-            %7D%0D%0A
         
@@ -1781,567 +1781,7 @@
  %7D%0D%0A
-%0D%0A    /**%0D%0A     * Decide whether a message body with the given Content-Type can be decoded%0D%0A     * as OAuth parameters.%0D%0A     */%0D%0A    protected static boolean isDecodable(String contentType)%0D%0A    %7B%0D%0A        if (contentType != null) %7B%0D%0A            int sep = contentType.indexOf(';');%0D%0A            String mimeType = (sep %3C 0) ? contentType : contentType.substring(0, sep);%0D%0A            mimeType = mimeType.trim();%0D%0A            if (%22text/html%22.equalsIgnoreCase(mimeType)) %7B%0D%0A                return false;%0D%0A            %7D%0D%0A        %7D%0D%0A        return true;%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

decode the response body regardless of its Content-Type