Search in sources :

Example 1 with ApplicationException

use of com.google.apphosting.api.ApiProxy.ApplicationException in project appengine-java-standard by GoogleCloudPlatform.

the class Modules method doStopModule.

private void doStopModule(String moduleName, String version) {
    Module module = getRequiredModule(moduleName);
    checkVersion(version, module);
    checkNotDynamicModule(module);
    try {
        module.stopServing();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "stopServing failed", e);
        throw new ApplicationException(ModulesServiceError.ErrorCode.UNEXPECTED_STATE_VALUE, "stopServing failed with error " + e.getMessage());
    }
}
Also used : ApplicationException(com.google.apphosting.api.ApiProxy.ApplicationException) ServletException(javax.servlet.ServletException) ApplicationException(com.google.apphosting.api.ApiProxy.ApplicationException) IOException(java.io.IOException)

Example 2 with ApplicationException

use of com.google.apphosting.api.ApiProxy.ApplicationException in project appengine-java-standard by GoogleCloudPlatform.

the class LocalDatastoreService method commit.

// status
@SuppressWarnings("unused")
public CommitResponse commit(Status status, final Transaction req) {
    Profile profile = profiles.get(req.getApp());
    checkNotNull(profile);
    CommitResponse response = new CommitResponse();
    globalLock.readLock().lock();
    // Synchronized so we can't commit and rollback at the same time.
    synchronized (profile) {
        LiveTxn liveTxn;
        try {
            liveTxn = profile.removeTxn(req.getHandle());
            try {
                if (liveTxn.isDirty()) {
                    response = commitImpl(liveTxn, profile);
                } else {
                    // cost of a read-only txn is 0
                    response.setCost(new Cost().setEntityWrites(0).setIndexWrites(0));
                }
            } catch (ApplicationException e) {
                // commit failed, re-add transaction so that it can be rolled back or reset.
                profile.addTxn(req.getHandle(), new LiveTxn(clock, liveTxn.allowMultipleEg, liveTxn.originalTransactionMode, true));
                throw e;
            }
        } finally {
            globalLock.readLock().unlock();
        }
        // taskqueue tasks become durable.
        for (TaskQueueAddRequest action : liveTxn.getActions()) {
            try {
                addActionImpl(action);
            } catch (ApplicationException e) {
                logger.log(Level.WARNING, "Transactional task: " + action + " has been dropped.", e);
            }
        }
    }
    return response;
}
Also used : ApplicationException(com.google.apphosting.api.ApiProxy.ApplicationException) TaskQueueAddRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest) CommitResponse(com.google.apphosting.datastore.DatastoreV3Pb.CommitResponse) Cost(com.google.apphosting.datastore.DatastoreV3Pb.Cost)

Example 3 with ApplicationException

use of com.google.apphosting.api.ApiProxy.ApplicationException in project appengine-java-standard by GoogleCloudPlatform.

the class LocalURLFetchService method fetch.

@LatencyPercentiles(latency50th = 5)
public URLFetchResponse fetch(Status status, URLFetchRequest request) {
    if (status == null) {
        throw new NullPointerException("status cannot be null.");
    }
    if (request == null) {
        throw new NullPointerException("request cannot be null.");
    }
    if (!hasValidURL(request)) {
        throw new ApplicationException(ErrorCode.INVALID_URL.getNumber(), "Invalid URL: " + request.getUrl());
    }
    MethodFactory methodFactory = METHOD_FACTORY_MAP.get(request.getMethod());
    if (methodFactory == null) {
        throw new ApplicationException(ErrorCode.INVALID_URL.getNumber(), "Unsupported method: " + request.getMethod());
    }
    HttpRequestBase method = methodFactory.buildMethod(request);
    HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, request.getFollowRedirects());
    // TODO set these timeouts according to the RPC deadline.
    // see http://b/1488459 for more info
    // how long we'll wait to establish a connection
    HttpConnectionParams.setConnectionTimeout(params, timeoutInMs);
    // how long we'll let the socket stay open
    HttpConnectionParams.setSoTimeout(params, timeoutInMs);
    method.setParams(params);
    boolean sawContentType = false;
    for (URLFetchRequest.Header pbHeader : request.getHeaderList()) {
        // an exception, and this behavior matches production.
        if (pbHeader.getKey().equalsIgnoreCase("Content-Length")) {
            continue;
        }
        method.addHeader(pbHeader.getKey(), pbHeader.getValue());
        if (pbHeader.getKey().equalsIgnoreCase("Content-Type")) {
            sawContentType = true;
        }
    }
    // TODO: Should we check on PUT/PATCH? What would the default be?
    if (!sawContentType && (request.getMethod() == RequestMethod.POST) && request.hasPayload()) {
        method.addHeader("Content-Type", "application/x-www-form-urlencoded");
    }
    URLFetchResponse.Builder response = URLFetchResponse.newBuilder();
    try {
        HttpResponse httpResponse = doPrivilegedExecute(request, method, response);
        int responseCode = httpResponse.getStatusLine().getStatusCode();
        if (responseCode < 100 || responseCode >= 600) {
            // Note, response codes in the range [100, 600) are valid.
            throw new ApplicationException(ErrorCode.FETCH_ERROR.getNumber(), "Status code " + responseCode + " unknown when making " + method.getMethod() + " request to URL: " + request.getUrl());
        }
        HttpEntity responseEntity = httpResponse.getEntity();
        if (responseEntity != null) {
            byte[] responseBuffer = responseToByteArray(responseEntity);
            if (responseBuffer.length > maxResponseLength) {
                responseBuffer = Arrays.copyOf(responseBuffer, maxResponseLength);
                response.setContentWasTruncated(true);
            }
            response.setContent(ByteString.copyFrom(responseBuffer));
        }
        httpclientHeadersToPbHeaders(httpResponse.getAllHeaders(), response);
    } catch (SocketTimeoutException ste) {
        throw new ApplicationException(ErrorCode.DEADLINE_EXCEEDED.getNumber(), "http method " + method.getMethod() + " against URL " + request.getUrl() + " timed out.");
    } catch (SSLException e) {
        throw new ApplicationException(ErrorCode.SSL_CERTIFICATE_ERROR.getNumber(), "Couldn't validate the server's SSL certificate for URL " + request.getUrl() + ": " + e.getMessage());
    } catch (IOException e) {
        if (e.getCause() != null && e.getCause().getMessage().matches("Maximum redirects \\([0-9]+\\) exceeded")) {
            throw new ApplicationException(ErrorCode.TOO_MANY_REDIRECTS.getNumber(), "Received exception executing http method " + method.getMethod() + " against URL " + request.getUrl() + ": " + e.getCause().getMessage());
        } else {
            throw new ApplicationException(ErrorCode.FETCH_ERROR.getNumber(), "Received exception executing http method " + method.getMethod() + " against URL " + request.getUrl() + ": " + e.getMessage());
        }
    }
    return response.build();
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) ApplicationException(com.google.apphosting.api.ApiProxy.ApplicationException) SocketTimeoutException(java.net.SocketTimeoutException) BasicHttpParams(org.apache.http.params.BasicHttpParams) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) LatencyPercentiles(com.google.appengine.tools.development.LatencyPercentiles)

Example 4 with ApplicationException

use of com.google.apphosting.api.ApiProxy.ApplicationException in project appengine-java-standard by GoogleCloudPlatform.

the class Modules method doStartModule.

private void doStartModule(String moduleName, String version) {
    Module module = getRequiredModule(moduleName);
    checkVersion(version, module);
    checkNotDynamicModule(module);
    try {
        module.startServing();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "startServing failed", e);
        throw new ApplicationException(ModulesServiceError.ErrorCode.UNEXPECTED_STATE_VALUE, "startServing failed with error " + e.getMessage());
    }
}
Also used : ApplicationException(com.google.apphosting.api.ApiProxy.ApplicationException) ServletException(javax.servlet.ServletException) ApplicationException(com.google.apphosting.api.ApiProxy.ApplicationException) IOException(java.io.IOException)

Aggregations

ApplicationException (com.google.apphosting.api.ApiProxy.ApplicationException)4 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)2 TaskQueueAddRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest)1 URLFetchRequest (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest)1 URLFetchResponse (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse)1 LatencyPercentiles (com.google.appengine.tools.development.LatencyPercentiles)1 CommitResponse (com.google.apphosting.datastore.DatastoreV3Pb.CommitResponse)1 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)1 SocketTimeoutException (java.net.SocketTimeoutException)1 SSLException (javax.net.ssl.SSLException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 BasicHttpParams (org.apache.http.params.BasicHttpParams)1 HttpParams (org.apache.http.params.HttpParams)1