Package org.jets3t.service

Examples of org.jets3t.service.ServiceException


                            response.getEntity().getContentLength()));
                }

                // Prepare exception with information about error response, whether or not it
                // contains XML content, in case we decide not to retry.
                ServiceException exception = null;

                if (isXmlContentType(contentType)
                    && response.getEntity() != null
                    && response.getEntity().getContentLength() != 0)
                {
                    // Prepare exception for XML-bearing error response
                    if(log.isDebugEnabled()) {
                        log.debug("Response '" + httpMethod.getURI().getRawPath()
                                + "' - Received error response with XML message");
                    }

                    StringBuilder sb = new StringBuilder();
                    BufferedReader reader = null;
                    try {
                        reader = new BufferedReader(new InputStreamReader(
                            new HttpMethodReleaseInputStream(response)));
                        String line;
                        while((line = reader.readLine()) != null) {
                            sb.append(line).append("\n");
                        }
                    }
                    finally {
                        if(reader != null) {
                            reader.close();
                        }
                    }
                    // Prepare exception containing the XML message document.
                    exception = new ServiceException("Service Error Message.", sb.toString());
                } else {
                    if(log.isDebugEnabled()) {
                        log.debug("Response '" + httpMethod.getURI().getRawPath()
                                + "' - Received error response without XML content");
                    }
                    String responseText = null;
                    byte[] responseBody = null;
                    if(response.getEntity() != null) {
                        responseBody = EntityUtils.toByteArray(response.getEntity());
                    }
                    if(responseBody != null && responseBody.length > 0) {
                        responseText = new String(responseBody);
                    }
                    // Prepare exception containing the HTTP error fields.
                    HttpException httpException = new HttpException(
                        responseCode, response.getStatusLine().getReasonPhrase());
                    exception = new ServiceException(
                        "Request Error" + (responseText != null ? " [" + responseText + "]." : "."),
                        httpException);
                }
                exception.setResponseCode(responseCode);
                exception.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders()));
                // Consume and release connection.
                EntityUtils.consume(response.getEntity());

                /*
                 * For cases where the request may succeed if retried, count the number of attempts
                 * we have made to ensure we don't exceeded the max retry limit.
                 */
                // Sleep then retry on 5xx Internal Server errors.
                if (responseCode >= 500) {
                    // Throws provided exception if we have exceeded the retry count
                    sleepOnInternalError(++internalErrorCount, exception);
                }
                // Retry after Temporary Redirect 307
                else if(responseCode == 307) {
                    // Retry up to our limit; but at least once to support S3 bucket locations
                    if (redirectCount >= retryMaxCount
                        && redirectCount > 0// Ensure we have retried at least once...
                    {
                        throw exception;
                    }

                    // Set new URI from Location header
                    Header locationHeader = response.getFirstHeader("location");
                    URI newLocation = new URI(locationHeader.getValue());
                    // deal with implementations of HttpUriRequest
                    if(httpMethod instanceof HttpRequestBase) {
                        ((HttpRequestBase) httpMethod).setURI(newLocation);
                    }
                    else if(httpMethod instanceof RequestWrapper) {
                        ((RequestWrapper) httpMethod).setURI(newLocation);
                    }

                    skipNextAuthorizationCycle = true;

                    redirectCount++;
                    if(log.isDebugEnabled()) {
                        log.debug(
                            "Following Temporary Redirect (" + redirectCount + ") to: "
                            + httpMethod.getURI().toString());
                    }
                }
                else if("RequestTimeout".equals(exception.getErrorCode())) {
                    if(requestTimeoutErrorCount >= retryMaxCount) {
                        throw exception;
                    }
                    requestTimeoutErrorCount++;
                    if(log.isWarnEnabled()) {
                        log.warn(
                            "Retrying connection that failed with RequestTimeout error ("
                            + requestTimeoutErrorCount + ")");
                    }
                }
                else if("RequestTimeTooSkewed".equals(exception.getErrorCode())) {
                    if(requestTimeTooSkewedErrorCount >= retryMaxCount) {
                        throw exception;
                    }
                    requestTimeTooSkewedErrorCount++;
                    this.timeOffset = RestUtils.calculateTimeAdjustmentOffset(response);
                    if(log.isWarnEnabled()) {
                        log.warn("Adjusted time offset in response to RequestTimeTooSkewed error"
                                + "(" + requestTimeTooSkewedErrorCount + ")."
                                + "Local machine and service disagree on the time by approximately "
                                + (this.timeOffset / 1000) + " seconds, please fix your system's time."
                                + " Retrying connection.");
                    }
                }
                // Special handling for S3 object PUT failures causing NoSuchKey errors - Issue #85, #175
                // Treat this as a special kind of internal server error.
                else if(responseCode == 404
                        && "PUT".equalsIgnoreCase(httpMethod.getMethod())
                        && "NoSuchKey".equals(exception.getErrorCode())
                        // If PUT operation is trying to copy an existing source object, don't ignore 404
                        && httpMethod.getFirstHeader(getRestHeaderPrefix() + "copy-source") == null) {
                    // Throws provided exception if we have exceeded the retry count
                    sleepOnInternalError(++internalErrorCount, exception);
                    if(log.isDebugEnabled()) {
                        log.debug("Ignoring NoSuchKey/404 error on PUT to: " + httpMethod.getURI().toString());
                    }
                }
                else if((responseCode == 403 || responseCode == 401) && this.isRecoverable403(httpMethod, exception)) {
                    // Retry up to our limit; but at least once
                    if (authFailureCount >= retryMaxCount
                        && authFailureCount > 0// Ensure we have retried at least once...
                    {
                        throw exception;
                    }
                    authFailureCount++;
                    if(log.isDebugEnabled()) {
                        log.debug("Retrying after 403 Forbidden");
                    }
                }
                // Special handling for requests to a region that requires AWS
                // request signature version 4 when service isn't configured to
                // use AWS4-HMAC-SHA256 signatures by default.
                else if ("InvalidRequest".equals(exception.getErrorCode())
                         && exception.getErrorMessage().contains("Please use AWS4-HMAC-SHA256"))
                {
                    forceRequestSignatureVersion = "AWS4-HMAC-SHA256";
                    authFailureCount++;
                    if (log.isWarnEnabled()) {
                        log.warn(
                            "Retrying request with \"AWS4-HMAC-SHA256\" signing"
                            + " mechanism: " + httpMethod);
                    }
                }
                // Special handling for requests signed using AWS request signature version
                // 4 but sent to the wrong region due to an incorrect Host endpoint.
                else if("AuthorizationHeaderMalformed".equals(exception.getErrorCode())) {
                    String expectedRegion = null;
                    try {
                        expectedRegion = exception.getXmlMessageAsBuilder()
                            .xpathFind("/Error/Region").getElement().getTextContent();
                    } catch(Exception ignored) {
                        // Throw original exception if we cannot parse expected
                        // Region out of error message, in which case this error
                        // was caused by something other than just wrong region.
                        throw exception;
                    }

                    // Cache correct region for this request's bucket name
                    this.regionEndpointCache.put(httpMethod, expectedRegion);

                    URI originalURI = httpMethod.getURI();
                    SignatureUtils.awsV4CorrectRequestHostForRegion(
                        httpMethod, expectedRegion);
                    authFailureCount++;

                    if(log.isWarnEnabled()) {
                        log.warn("Retrying request after automatic adjustment of"
                            + " Host endpoint from " + "\"" + originalURI.getHost()
                            + "\" to \"" + httpMethod.getURI().getHost()
                            + "\" following request signing error"
                            + " using AWS request signing version 4: " + httpMethod);
                    }
                }
                // If we haven't explicitly detected an retry-able error response type above,
                // just throw the exception to abort the request.
                else {
                    throw exception;
                }

                // Print warning message if a non-fatal error occurred (we only reach this
                // point in the code if an exception isn't thrown above)
                if(log.isWarnEnabled()) {
                    String requestDescription =
                        httpMethod.getMethod()
                            + " '" + httpMethod.getURI().getPath()
                            + (httpMethod.getURI().getQuery() != null
                            && httpMethod.getURI().getQuery().length() > 0
                            ? "?" + httpMethod.getURI().getQuery() : "")
                            + "'"
                            + " -- ResponseCode: " + responseCode
                            + ", ResponseStatus: " + response.getStatusLine().getReasonPhrase()
                            + ", Request Headers: [" + ServiceUtils.join(httpMethod.getAllHeaders(), ", ") + "]"
                            + ", Response Headers: [" + ServiceUtils.join(response.getAllHeaders(), ", ") + "]";
                    requestDescription = requestDescription.replaceAll("[\\n\\r\\f]", "")// Remove any newlines.
                    log.warn("Retrying request following error response: " + requestDescription);
                }
            }
        }
        // Top-level exception handler to deal with otherwise unhandled exceptions, or to augment
        // a ServiceException thrown above with additional information.
        catch(Exception t) {
            if(log.isDebugEnabled()) {
                String msg = "Rethrowing as a ServiceException error in performRequest: " + t;
                if(t.getCause() != null) {
                    msg += ", with cause: " + t.getCause();
                }
                if(log.isTraceEnabled()) {
                    log.trace(msg, t);
                }
                else {
                    log.debug(msg);
                }
            }
            if(log.isDebugEnabled() && !shuttingDown) {
                log.debug("Releasing HttpClient connection after error: " + t.getMessage());
            }
            httpMethod.abort();

            ServiceException serviceException;
            if(t instanceof ServiceException) {
                serviceException = (ServiceException) t;
            }
            else {
                MxDelegate.getInstance().registerS3ServiceExceptionEvent();
                serviceException = new ServiceException("Request Error: " + t.getMessage(), t);
            }

            // Add S3 request and host IDs from HTTP headers to exception, if they are available
            // and have not already been populated by parsing an XML error response.
            if(!serviceException.isParsedFromXmlMessage()
                    && response != null
                    && response.getFirstHeader(Constants.AMZ_REQUEST_ID_1) != null
                    && response.getFirstHeader(Constants.AMZ_REQUEST_ID_2) != null) {
                serviceException.setRequestAndHostIds(
                        response.getFirstHeader(Constants.AMZ_REQUEST_ID_1).getValue(),
                        response.getFirstHeader(Constants.AMZ_REQUEST_ID_2).getValue());
                serviceException.setResponseHeaders(RestUtils.convertHeadersToMap(
                        response.getAllHeaders()));
            }
            if(response != null && response.getStatusLine() != null) {
                // If no network connection is available, status info is not available
                serviceException.setResponseCode(response.getStatusLine().getStatusCode());
                serviceException.setResponseStatus(response.getStatusLine().getReasonPhrase());
            }
            if(httpMethod.getFirstHeader("Host") != null) {
                serviceException.setRequestHost(
                        httpMethod.getFirstHeader("Host").getValue());
            }
            if(response != null && response.getFirstHeader("Date") != null) {
                serviceException.setResponseDate(
                        response.getFirstHeader("Date").getValue());
            }
            throw serviceException;
        }
        return response;
View Full Code Here


    {
        final List<StorageObject> allObjects =
            Collections.synchronizedList(new ArrayList<StorageObject>());
        final List<String> lastCommonPrefixes =
            Collections.synchronizedList(new ArrayList<String>());
        final ServiceException serviceExceptions[] = new ServiceException[1];

        /*
         * Create a ThreadedStorageService object with an event listener that responds to
         * ListObjectsEvent notifications and populates a complete object listing.
         */
        final ThreadedStorageService threadedService = new ThreadedStorageService(service,
            new StorageServiceEventAdaptor() {
            @Override
            public void event(ListObjectsEvent event) {
                if (ListObjectsEvent.EVENT_IN_PROGRESS == event.getEventCode()) {
                    Iterator<StorageObjectsChunk> chunkIter = event.getChunkList().iterator();
                    while (chunkIter.hasNext()) {
                        StorageObjectsChunk chunk = chunkIter.next();

                        if (log.isDebugEnabled()) {
                            log.debug("Listed " + chunk.getObjects().length
                            + " objects and " + chunk.getCommonPrefixes().length
                            + " common prefixes in bucket '" + bucketName
                            + "' using prefix=" + chunk.getPrefix()
                            + ", delimiter=" + chunk.getDelimiter());
                        }

                        allObjects.addAll(Arrays.asList(chunk.getObjects()));
                        lastCommonPrefixes.addAll(Arrays.asList(chunk.getCommonPrefixes()));
                    }
                } else if (ListObjectsEvent.EVENT_ERROR == event.getEventCode()) {
                    serviceExceptions[0] = new ServiceException(
                        "Failed to list all objects in bucket",
                        event.getErrorCause());
                }
            }
        });
View Full Code Here

        String bucketListingProperties = jets3tProperties.getStringProperty(
            "filecomparer.bucket-listing." + bucketName, null);
        if (bucketListingProperties != null) {
            String splits[] = bucketListingProperties.split(",");
            if (splits.length != 2) {
                throw new ServiceException(
                    "Invalid setting for bucket listing property "
                    + "filecomparer.bucket-listing." + bucketName + ": '" +
                    bucketListingProperties + "'");
            }
            delimiter = splits[0].trim();
View Full Code Here

                } else {
                    fileHashAsHex = ServiceUtils.toHex(
                        generateFileMD5Hash(file, objectKey, progressWatcher));
                }
            } catch (Exception e) {
                throw new ServiceException(
                    "Unable to generate MD5 hash for file " + file.getPath(), e);
            }

            if (object.getETag() != null && object.getETag().equals(fileHashAsHex)) {
                // Object's ETag value is available and matches the MD5 hex hash of the file
                continue;
            }
            // Cannot tell whether local file and object are the same,
            // we will need all the object's metadata.
            objectsForMetadataRetrieval.add(object);
        }

        if (objectsForMetadataRetrieval.size() > 0) {
            // Retrieve the complete metadata information for selected objects
            final List<StorageObject> objectsCompleteList =
                new ArrayList<StorageObject>(objectsWithoutMetadata.length);
            final ServiceException serviceExceptions[] = new ServiceException[1];
            ThreadedStorageService threadedService = new ThreadedStorageService(service,
                new StorageServiceEventAdaptor() {
                @Override
                public void event(GetObjectHeadsEvent event) {
                    if (GetObjectHeadsEvent.EVENT_IN_PROGRESS == event.getEventCode()) {
                        StorageObject[] finishedObjects = event.getCompletedObjects();
                        if (finishedObjects.length > 0) {
                            objectsCompleteList.addAll(Arrays.asList(finishedObjects));
                        }
                    } else if (GetObjectHeadsEvent.EVENT_ERROR == event.getEventCode()) {
                        serviceExceptions[0] = new ServiceException(
                            "Failed to retrieve detailed information about all objects",
                            event.getErrorCause());
                    }
                }
            });
View Full Code Here

     */
    public String toXml() throws ServiceException {
        try {
            return toXMLBuilder().asString();
        } catch (Exception e) {
            throw new ServiceException("Failed to build XML document for ACL", e);
        }
    }
View Full Code Here

        BufferedInputStream fileIS = null;
        try {
            fileIS = new BufferedInputStream(new FileInputStream(file));
            return load(password, fileIS);
        } catch (Throwable t) {
            throw new ServiceException("Failed to load credentials", t);
        } finally {
            if (fileIS != null) {
                try {
                    fileIS.close();
                } catch (IOException e) {
View Full Code Here

                return new AWSDevPayCredentials(parts[0], parts[1], parts[2], parts[3], friendlyName);
            } else {
                return new AWSCredentials(parts[0], parts[1], friendlyName);
            }
        } catch (BadPaddingException bpe) {
            throw new ServiceException("Unable to decrypt credentials. Is your password correct?", bpe);
        } catch (Throwable t) {
            throw new ServiceException("Failed to load credentials", t);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
View Full Code Here

            } catch (SAXException e) {
                // Ignore failure
            }
        }
        // If we haven't found and returned an XMLReader yet, give up.
        throw new ServiceException("Failed to initialize a SAX XMLReader");
    }
View Full Code Here

            // MultipartUpload objects were captured by this method's
            // captureMultipartUploadObjectsEventAdaptor)
            for (MultipartUpload upload: multipartUploadList) {
                StorageObject object = objectsByKey.get(upload.getObjectKey());
                if (object.getDataInputFile() == null) {
                    throw new ServiceException();
                }
                partObjects = splitFileIntoObjectsByMaxPartSize(
                    upload.getObjectKey(),
                    object.getDataInputFile());
                uploadAndPartsList.add(
View Full Code Here

        if (dataInputStream == null && dataInputFile != null) {
            try {
                // Use a repeatable file data input stream, so transmissions can be retried if necessary.
                dataInputStream = new RepeatableFileInputStream(dataInputFile);
            } catch (FileNotFoundException e) {
                throw new ServiceException("Cannot open file input stream", e);
            }
        }
        return dataInputStream;
    }
View Full Code Here

TOP

Related Classes of org.jets3t.service.ServiceException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.