| 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 static com.google.common.base.Throwables.propagate; |
| 22 | import static org.jclouds.http.HttpUtils.checkRequestHasContentLengthOrChunkedEncoding; |
| 23 | import static org.jclouds.http.HttpUtils.wirePayloadIfEnabled; |
| 24 | |
| 25 | import java.io.IOException; |
| 26 | import java.util.concurrent.ExecutorService; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Named; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.Constants; |
| 34 | import org.jclouds.concurrent.Futures; |
| 35 | import org.jclouds.concurrent.SingleThreaded; |
| 36 | import org.jclouds.http.HttpCommand; |
| 37 | import org.jclouds.http.HttpCommandExecutorService; |
| 38 | import org.jclouds.http.HttpRequest; |
| 39 | import org.jclouds.http.HttpRequestFilter; |
| 40 | import org.jclouds.http.HttpResponse; |
| 41 | import org.jclouds.http.HttpResponseException; |
| 42 | import org.jclouds.http.HttpUtils; |
| 43 | import org.jclouds.http.IOExceptionRetryHandler; |
| 44 | import org.jclouds.http.handlers.DelegatingErrorHandler; |
| 45 | import org.jclouds.http.handlers.DelegatingRetryHandler; |
| 46 | import org.jclouds.http.internal.HttpWire; |
| 47 | import org.jclouds.logging.Logger; |
| 48 | import org.jclouds.util.Throwables2; |
| 49 | |
| 50 | import com.google.appengine.api.urlfetch.HTTPRequest; |
| 51 | import com.google.appengine.api.urlfetch.URLFetchService; |
| 52 | import com.google.common.base.Function; |
| 53 | import com.google.common.util.concurrent.ListenableFuture; |
| 54 | |
| 55 | /** |
| 56 | * Google App Engine version of {@link HttpCommandExecutorService} using their fetchAsync call |
| 57 | * |
| 58 | * @author Adrian Cole |
| 59 | */ |
| 60 | @SingleThreaded |
| 61 | @Singleton |
| 62 | public class AsyncGaeHttpCommandExecutorService implements HttpCommandExecutorService { |
| 63 | private final ExecutorService service; |
| 64 | private final URLFetchService urlFetchService; |
| 65 | private final ConvertToGaeRequest convertToGaeRequest; |
| 66 | private final ConvertToJcloudsResponse convertToJcloudsResponse; |
| 67 | private final DelegatingRetryHandler retryHandler; |
| 68 | private final IOExceptionRetryHandler ioRetryHandler; |
| 69 | private final DelegatingErrorHandler errorHandler; |
| 70 | |
| 71 | @Resource |
| 72 | protected Logger logger = Logger.NULL; |
| 73 | @Resource |
| 74 | @Named(Constants.LOGGER_HTTP_HEADERS) |
| 75 | protected Logger headerLog = Logger.NULL; |
| 76 | protected final HttpWire wire; |
| 77 | protected final HttpUtils utils; |
| 78 | |
| 79 | @Inject |
| 80 | public AsyncGaeHttpCommandExecutorService(@Named(Constants.PROPERTY_USER_THREADS) ExecutorService service, |
| 81 | URLFetchService urlFetchService, ConvertToGaeRequest convertToGaeRequest, |
| 82 | ConvertToJcloudsResponse convertToJcloudsResponse, DelegatingRetryHandler retryHandler, |
| 83 | IOExceptionRetryHandler ioRetryHandler, DelegatingErrorHandler errorHandler, HttpUtils utils, HttpWire wire) { |
| 84 | this.service = service; |
| 85 | this.urlFetchService = urlFetchService; |
| 86 | this.convertToGaeRequest = convertToGaeRequest; |
| 87 | this.convertToJcloudsResponse = convertToJcloudsResponse; |
| 88 | this.retryHandler = retryHandler; |
| 89 | this.ioRetryHandler = ioRetryHandler; |
| 90 | this.errorHandler = errorHandler; |
| 91 | this.utils = utils; |
| 92 | this.wire = wire; |
| 93 | } |
| 94 | |
| 95 | public HTTPRequest filterLogAndConvertRe(HttpRequest request) { |
| 96 | |
| 97 | for (HttpRequestFilter filter : request.getFilters()) { |
| 98 | request = filter.filter(request); |
| 99 | } |
| 100 | checkRequestHasContentLengthOrChunkedEncoding(request, |
| 101 | "After filtering, the request has niether chunked encoding nor content length: " + request); |
| 102 | logger.debug("Sending request %s: %s", request.hashCode(), request.getRequestLine()); |
| 103 | wirePayloadIfEnabled(wire, request); |
| 104 | HTTPRequest nativeRequest = convertToGaeRequest.apply(request); |
| 105 | utils.logRequest(headerLog, request, ">>"); |
| 106 | return nativeRequest; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public ListenableFuture<HttpResponse> submit(final HttpCommand command) { |
| 111 | |
| 112 | HTTPRequest nativeRequest = filterLogAndConvertRe(command.getCurrentRequest()); |
| 113 | |
| 114 | ListenableFuture<HttpResponse> response = Futures.compose(urlFetchService.fetchAsync(nativeRequest), |
| 115 | convertToJcloudsResponse, service); |
| 116 | |
| 117 | return Futures.compose(response, new Function<HttpResponse, HttpResponse>() { |
| 118 | |
| 119 | @Override |
| 120 | public HttpResponse apply(HttpResponse response) { |
| 121 | try { |
| 122 | logger.debug("Receiving response %s: %s", command.getCurrentRequest().hashCode(), |
| 123 | response.getStatusLine()); |
| 124 | utils.logResponse(headerLog, response, "<<"); |
| 125 | if (response.getPayload() != null && wire.enabled()) |
| 126 | wire.input(response); |
| 127 | int statusCode = response.getStatusCode(); |
| 128 | if (statusCode >= 300) { |
| 129 | if (shouldContinue(response)) |
| 130 | return submit(command).get(); |
| 131 | else |
| 132 | return response; |
| 133 | } |
| 134 | return response; |
| 135 | } catch (Exception e) { |
| 136 | IOException ioe = Throwables2.getFirstThrowableOfType(e, IOException.class); |
| 137 | if (ioe != null && ioRetryHandler.shouldRetryRequest(command, ioe)) { |
| 138 | try { |
| 139 | return submit(command).get(); |
| 140 | } catch (Exception e1) { |
| 141 | command.setException(e1); |
| 142 | return response; |
| 143 | } |
| 144 | } else { |
| 145 | command.setException(new HttpResponseException(e.getMessage() + " connecting to " |
| 146 | + command.getCurrentRequest().getRequestLine(), command, null, e)); |
| 147 | return response; |
| 148 | } |
| 149 | } finally { |
| 150 | if (command.getException() != null) |
| 151 | propagate(command.getException()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private boolean shouldContinue(HttpResponse response) { |
| 156 | boolean shouldContinue = false; |
| 157 | if (retryHandler.shouldRetryRequest(command, response)) { |
| 158 | shouldContinue = true; |
| 159 | } else { |
| 160 | errorHandler.handleError(command, response); |
| 161 | } |
| 162 | return shouldContinue; |
| 163 | } |
| 164 | }, service); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | } |