/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. package com.google.ie.common.openid.appengine;
  16. import com.google.appengine.api.urlfetch.HTTPHeader;
  17. import com.google.appengine.api.urlfetch.HTTPMethod;
  18. import com.google.appengine.api.urlfetch.HTTPRequest;
  19. import com.google.appengine.api.urlfetch.HTTPResponse;
  20. import com.google.appengine.api.urlfetch.URLFetchService;
  21. import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
  22. import com.google.inject.Singleton;
  23. import com.google.step2.http.FetchException;
  24. import com.google.step2.http.FetchRequest;
  25. import com.google.step2.http.FetchResponse;
  26. import com.google.step2.http.HttpFetcher;
  27. import java.io.ByteArrayInputStream;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.net.MalformedURLException;
  31. import java.util.List;
  32. @Singleton
  33. public class AppEngineHttpFetcher implements HttpFetcher {
  34. private final URLFetchService fetchService;
  35. public AppEngineHttpFetcher() {
  36. fetchService = URLFetchServiceFactory.getURLFetchService();
  37. }
  38. public FetchResponse fetch(FetchRequest request) throws FetchException {
  39. HTTPMethod method;
  40. switch (request.getMethod()) {
  41. case POST:
  42. method = HTTPMethod.POST;
  43. break;
  44. case HEAD:
  45. method = HTTPMethod.HEAD;
  46. break;
  47. default:
  48. method = HTTPMethod.GET;
  49. break;
  50. }
  51. try {
  52. HTTPRequest httpRequest = new HTTPRequest(request.getUri().toURL(), method);
  53. HTTPResponse httpResponse = fetchService.fetch(httpRequest);
  54. return new AppEngineFetchResponse(httpResponse);
  55. } catch (MalformedURLException e) {
  56. throw new FetchException(e);
  57. } catch (IOException e) {
  58. throw new FetchException(e);
  59. }
  60. }
  61. private static class AppEngineFetchResponse implements FetchResponse {
  62. private final HTTPResponse httpResponse;
  63. public AppEngineFetchResponse(HTTPResponse httpResponse) {
  64. this.httpResponse = httpResponse;
  65. }
  66. public byte[] getContentAsBytes() {
  67. return httpResponse.getContent();
  68. }
  69. public InputStream getContentAsStream() {
  70. return new ByteArrayInputStream(getContentAsBytes());
  71. }
  72. public String getFirstHeader(String name) {
  73. List<HTTPHeader> headers = httpResponse.getHeaders();
  74. for (HTTPHeader header : headers) {
  75. if (header.getName().equalsIgnoreCase(name)) {
  76. return header.getValue();
  77. }
  78. }
  79. return null;
  80. }
  81. public int getStatusCode() {
  82. return httpResponse.getResponseCode();
  83. }
  84. }
  85. }