Home | History | Annotate | Download | only in client
      1 /*
      2  * Copyright 2008 Netflix, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package net.oauth.client;
     18 
     19 import java.io.IOException;
     20 import java.io.InputStream;
     21 import java.util.Map;
     22 import net.oauth.OAuth;
     23 import net.oauth.OAuthMessage;
     24 import net.oauth.OAuthProblemException;
     25 import net.oauth.http.HttpMessage;
     26 import net.oauth.http.HttpResponseMessage;
     27 
     28 /**
     29  * An HTTP response, encapsulated as an OAuthMessage.
     30  *
     31  * @author John Kristian
     32  * @hide
     33  */
     34 final class OAuthResponseMessage extends OAuthMessage
     35 {
     36     OAuthResponseMessage(HttpResponseMessage http) throws IOException
     37     {
     38         super(http.method, http.url.toExternalForm(), null);
     39         this.http = http;
     40         getHeaders().addAll(http.headers);
     41         for (Map.Entry<String, String> header : http.headers) {
     42             if ("WWW-Authenticate".equalsIgnoreCase(header.getKey())) {
     43                 for (OAuth.Parameter parameter : decodeAuthorization(header.getValue())) {
     44                     if (!"realm".equalsIgnoreCase(parameter.getKey())) {
     45                         addParameter(parameter);
     46                     }
     47                 }
     48             }
     49         }
     50     }
     51 
     52     private final HttpMessage http;
     53 
     54     @Override
     55     public InputStream getBodyAsStream() throws IOException
     56     {
     57         return http.getBody();
     58     }
     59 
     60     @Override
     61     public String getBodyEncoding()
     62     {
     63         return http.getContentCharset();
     64     }
     65 
     66     @Override
     67     protected void completeParameters() throws IOException
     68     {
     69         super.completeParameters();
     70         String body = readBodyAsString();
     71         if (body != null) {
     72             addParameters(OAuth.decodeForm(body.trim()));
     73         }
     74     }
     75 
     76     @Override
     77     protected void dump(Map<String, Object> into) throws IOException
     78     {
     79         super.dump(into);
     80         http.dump(into);
     81     }
     82 
     83     @Override
     84     public void requireParameters(String... names) throws OAuthProblemException, IOException {
     85         try {
     86             super.requireParameters(names);
     87         } catch (OAuthProblemException problem) {
     88             problem.getParameters().putAll(getDump());
     89             throw problem;
     90         }
     91     }
     92 
     93 }
     94