| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.gae; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.util.concurrent.ExecutorService; |
| 23 | |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Named; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.Constants; |
| 29 | import org.jclouds.concurrent.SingleThreaded; |
| 30 | import org.jclouds.http.HttpCommandExecutorService; |
| 31 | import org.jclouds.http.HttpRequest; |
| 32 | import org.jclouds.http.HttpResponse; |
| 33 | import org.jclouds.http.HttpUtils; |
| 34 | import org.jclouds.http.IOExceptionRetryHandler; |
| 35 | import org.jclouds.http.handlers.DelegatingErrorHandler; |
| 36 | import org.jclouds.http.handlers.DelegatingRetryHandler; |
| 37 | import org.jclouds.http.internal.BaseHttpCommandExecutorService; |
| 38 | import org.jclouds.http.internal.HttpWire; |
| 39 | |
| 40 | import com.google.appengine.api.urlfetch.HTTPRequest; |
| 41 | import com.google.appengine.api.urlfetch.HTTPResponse; |
| 42 | import com.google.appengine.api.urlfetch.URLFetchService; |
| 43 | import com.google.common.annotations.VisibleForTesting; |
| 44 | |
| 45 | /** |
| 46 | * Google App Engine version of {@link HttpCommandExecutorService} |
| 47 | * |
| 48 | * @author Adrian Cole |
| 49 | */ |
| 50 | @SingleThreaded |
| 51 | @Singleton |
| 52 | public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorService<HTTPRequest> { |
| 53 | public static final String USER_AGENT = "jclouds/1.0 urlfetch/1.3.5"; |
| 54 | |
| 55 | private final URLFetchService urlFetchService; |
| 56 | private final ConvertToGaeRequest convertToGaeRequest; |
| 57 | private final ConvertToJcloudsResponse convertToJcloudsResponse; |
| 58 | |
| 59 | @Inject |
| 60 | public GaeHttpCommandExecutorService(URLFetchService urlFetchService, HttpUtils utils, |
| 61 | @Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioExecutor, |
| 62 | IOExceptionRetryHandler ioRetryHandler, DelegatingRetryHandler retryHandler, |
| 63 | DelegatingErrorHandler errorHandler, HttpWire wire, ConvertToGaeRequest convertToGaeRequest, |
| 64 | ConvertToJcloudsResponse convertToJcloudsResponse) { |
| 65 | super(utils, ioExecutor, retryHandler, ioRetryHandler, errorHandler, wire); |
| 66 | this.urlFetchService = urlFetchService; |
| 67 | this.convertToGaeRequest = convertToGaeRequest; |
| 68 | this.convertToJcloudsResponse = convertToJcloudsResponse; |
| 69 | } |
| 70 | |
| 71 | @VisibleForTesting |
| 72 | protected HttpResponse convert(HTTPResponse gaeResponse) { |
| 73 | return convertToJcloudsResponse.apply(gaeResponse); |
| 74 | } |
| 75 | |
| 76 | @VisibleForTesting |
| 77 | protected HTTPRequest convert(HttpRequest request) throws IOException { |
| 78 | return convertToGaeRequest.apply(request); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * nothing to clean up. |
| 83 | */ |
| 84 | @Override |
| 85 | protected void cleanup(HTTPRequest nativeRequest) { |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected HttpResponse invoke(HTTPRequest request) throws IOException { |
| 90 | return convert(urlFetchService.fetch(request)); |
| 91 | } |
| 92 | } |