PageRenderTime 26ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/google/ie/common/openid/appengine/AppEngineHttpFetcher.java

http://thoughtsite.googlecode.com/
Java | 104 lines | 71 code | 19 blank | 14 comment | 3 complexity | 436873eb44cf196488e4dbfd1c47bdbd MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 * 
  7 *      http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16package com.google.ie.common.openid.appengine;
 17
 18import com.google.appengine.api.urlfetch.HTTPHeader;
 19import com.google.appengine.api.urlfetch.HTTPMethod;
 20import com.google.appengine.api.urlfetch.HTTPRequest;
 21import com.google.appengine.api.urlfetch.HTTPResponse;
 22import com.google.appengine.api.urlfetch.URLFetchService;
 23import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
 24import com.google.inject.Singleton;
 25import com.google.step2.http.FetchException;
 26import com.google.step2.http.FetchRequest;
 27import com.google.step2.http.FetchResponse;
 28import com.google.step2.http.HttpFetcher;
 29
 30import java.io.ByteArrayInputStream;
 31import java.io.IOException;
 32import java.io.InputStream;
 33import java.net.MalformedURLException;
 34import java.util.List;
 35
 36@Singleton
 37public class AppEngineHttpFetcher implements HttpFetcher {
 38
 39    private final URLFetchService fetchService;
 40
 41    public AppEngineHttpFetcher() {
 42        fetchService = URLFetchServiceFactory.getURLFetchService();
 43    }
 44
 45    public FetchResponse fetch(FetchRequest request) throws FetchException {
 46
 47        HTTPMethod method;
 48        switch (request.getMethod()) {
 49            case POST:
 50                method = HTTPMethod.POST;
 51                break;
 52            case HEAD:
 53                method = HTTPMethod.HEAD;
 54                break;
 55            default:
 56                method = HTTPMethod.GET;
 57                break;
 58        }
 59
 60        try {
 61
 62            HTTPRequest httpRequest = new HTTPRequest(request.getUri().toURL(), method);
 63            HTTPResponse httpResponse = fetchService.fetch(httpRequest);
 64            return new AppEngineFetchResponse(httpResponse);
 65
 66        } catch (MalformedURLException e) {
 67            throw new FetchException(e);
 68        } catch (IOException e) {
 69            throw new FetchException(e);
 70        }
 71    }
 72
 73    private static class AppEngineFetchResponse implements FetchResponse {
 74
 75        private final HTTPResponse httpResponse;
 76
 77        public AppEngineFetchResponse(HTTPResponse httpResponse) {
 78            this.httpResponse = httpResponse;
 79        }
 80
 81        public byte[] getContentAsBytes() {
 82            return httpResponse.getContent();
 83        }
 84
 85        public InputStream getContentAsStream() {
 86            return new ByteArrayInputStream(getContentAsBytes());
 87        }
 88
 89        public String getFirstHeader(String name) {
 90            List<HTTPHeader> headers = httpResponse.getHeaders();
 91            for (HTTPHeader header : headers) {
 92                if (header.getName().equalsIgnoreCase(name)) {
 93                    return header.getValue();
 94                }
 95            }
 96            return null;
 97        }
 98
 99        public int getStatusCode() {
100            return httpResponse.getResponseCode();
101        }
102    }
103}
104