Package org.jets3t.service

Examples of org.jets3t.service.S3ServiceException


       
        StringBuffer detailsText = new StringBuffer();
        if (throwable instanceof S3ServiceException) {
            detailsText.append("<table border=\"0\">");
           
            S3ServiceException s3se = (S3ServiceException) throwable;
           
            if (s3se.getS3ErrorCode() != null) {
                detailsText.append("<tr><td><b>S3 Error Code</b></td><td>").append(s3se.getS3ErrorCode()).append("</td></tr>");
            } else {
                detailsText.append("<tr><td><b>Exception message</b></td></tr><tr><td>")
                    .append(throwable.getMessage()).append("</td></tr>");
            }
           
            if (s3se.getS3ErrorMessage() != null) {
                detailsText.append("<tr><td><b>S3 Message</b></td><td>")
                    .append(s3se.getS3ErrorMessage()).append("</td></tr>");
            }
           
            if (s3se.getS3ErrorRequestId() != null) {
                detailsText.append("<tr><td><b>S3 Request Id</b></td><td>")
                    .append(s3se.getS3ErrorRequestId()).append("</td></tr>");
            }

            if (s3se.getS3ErrorHostId() != null) {
                detailsText.append("<tr><td><b>S3 Host Id</b></td><td>")
                    .append(s3se.getS3ErrorHostId()).append("</td></tr>");
            }

            boolean firstCause = true;
            Throwable cause = s3se.getCause();
            while (cause != null && cause.getMessage() != null) {
                if (firstCause) {
                    detailsText.append("<tr><td><b>Cause</b></td></tr>");
                }
                detailsText.append("<tr><td>").append(cause.getMessage()).append("</td></tr>");
View Full Code Here


                    completedWithoutRecoverableError = false;
                    redirectCount++;
                    wasRecentlyRedirected = true;
                   
                    if (redirectCount > 5) {
                        throw new S3ServiceException("Encountered too many 307 Redirects, aborting request.");
                    }
                } else if (responseCode == 500) {
                    // Retry on S3 Internal Server 500 errors.
                    completedWithoutRecoverableError = false;
                    sleepOnInternalError(++internalErrorCount);
                } else {
                    completedWithoutRecoverableError = true;                   
                }

                String contentType = "";
                if (httpMethod.getResponseHeader("Content-Type") != null) {
                    contentType = httpMethod.getResponseHeader("Content-Type").getValue();
                }
               
                log.debug("Response for '" + httpMethod.getPath()
                    + "'. Content-Type: " + contentType
                    + ", Headers: " + Arrays.asList(httpMethod.getResponseHeaders()));
                       
                // Check we received the expected result code.
                if (responseCode != expectedResponseCode) {               
                    log.warn("Response '" + httpMethod.getPath() + "' - Unexpected response code "
                        + responseCode + ", expected " + expectedResponseCode);
                                       
                    if (Mimetypes.MIMETYPE_XML.equals(contentType)
                        && httpMethod.getResponseBodyAsStream() != null
                        && httpMethod.getResponseContentLength() != 0)
                    {
                        log.warn("Response '" + httpMethod.getPath()
                            + "' - Received error response with XML message");
       
                        StringBuffer sb = new StringBuffer();
                        BufferedReader reader = null;
                        try {
                            reader = new BufferedReader(new InputStreamReader(
                                new HttpMethodReleaseInputStream(httpMethod)));
                            String line = null;
                            while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                            }
                        } finally {
                            if (reader != null) {
                                reader.close();                               
                            }                           
                        }
                       
                        httpMethod.releaseConnection();
                       
                        // Throw exception containing the XML message document.
                        S3ServiceException exception =
                            new S3ServiceException("S3 " + httpMethod.getName()
                                + " failed for '" + httpMethod.getPath() + "'", sb.toString());
                       
                        if ("RequestTimeout".equals(exception.getS3ErrorCode())) {
                            int retryMaxCount = Jets3tProperties
                                .getInstance(Constants.JETS3T_PROPERTIES_FILENAME)
                                .getIntProperty("httpclient.retry-max", 5);                           
                           
                            if (requestTimeoutErrorCount < retryMaxCount) {
                                requestTimeoutErrorCount++;                               
                                log.warn("Response '" + httpMethod.getPath()
                                    + "' - Retrying connection that failed with RequestTimeout error"
                                    + ", attempt number " + requestTimeoutErrorCount + " of "
                                    + retryMaxCount);
                                completedWithoutRecoverableError = false;
                            } else {
                                log.warn("Response '" + httpMethod.getPath()
                                    + "' - Exceeded maximum number of retries for RequestTimeout errors: "
                                    + retryMaxCount);
                                throw exception;
                            }
                        } else if ("RequestTimeTooSkewed".equals(exception.getS3ErrorCode())) {
                            long timeDifferenceMS = adjustTime();
                            log.warn("Adjusted time offset in response to RequestTimeTooSkewed error. "
                                + "Local machine and S3 server disagree on the time by approximately "
                                + (timeDifferenceMS / 1000) + " seconds. Retrying connection.");
                            completedWithoutRecoverableError = false;
                        } else if (responseCode == 500) {
                            // Retrying after InternalError 500, don't throw exception.
                        } else if (responseCode == 307) {
                            // Retrying after Temporary Redirect 307, don't throw exception.
                            log.debug("Following Temporary Redirect to: " + httpMethod.getURI().toString());                           
                        } else {
                            throw exception;                           
                        }
                    } else {                       
                        // Consume response content and release connection.
                        String responseText = null;
                        byte[] responseBody = httpMethod.getResponseBody();
                        if (responseBody != null && responseBody.length > 0) {
                            responseText = new String(responseBody);
                        }
   
                        log.debug("Releasing error response without XML content");
                        httpMethod.releaseConnection();
                       
                        if (responseCode == 500) {
                            // Retrying after InternalError 500, don't throw exception.
                        } else {
                            // Throw exception containing the HTTP error fields.
                          HttpException httpException = new HttpException(
                              httpMethod.getStatusCode(), httpMethod.getStatusText());
                            throw new S3ServiceException("S3 " + httpMethod.getName()
                                + " request failed for '" + httpMethod.getPath() + "' - "
                                + "ResponseCode=" + httpMethod.getStatusCode()
                                + ", ResponseMessage=" + httpMethod.getStatusText()
                                + (responseText != null ? "\n" + responseText : ""),
                                httpException);
                        }
                    }
                }
            } while (!completedWithoutRecoverableError);

            // Release immediately any connections without response bodies.
            if ((httpMethod.getResponseBodyAsStream() == null
                || httpMethod.getResponseBodyAsStream().available() == 0)
                && httpMethod.getResponseContentLength() == 0)
            {
                log.debug("Releasing response without content");
                byte[] responseBody = httpMethod.getResponseBody();

                if (responseBody != null && responseBody.length > 0)
                    throw new S3ServiceException("Oops, too keen to release connection with a non-empty response body");               
                httpMethod.releaseConnection();
            }
           
        } catch (S3ServiceException e) {
            throw e;
        } catch (Throwable t) {
            log.debug("Releasing method after error: " + t.getMessage());           
            httpMethod.releaseConnection();
            throw new S3ServiceException("S3 " + httpMethod.getName()
                + " connection failed for '" + httpMethod.getPath() + "'", t);
        }
    }
View Full Code Here

     * @throws S3ServiceException
     */
    protected HttpMethodBase setupConnection(String method, String bucketName, String objectKey, Map requestParameters) throws S3ServiceException
    {
        if (bucketName == null) {
            throw new S3ServiceException("Cannot connect to S3 Service with a null path");
        }

        String hostname = generateS3HostnameForBucket(bucketName);

    // Determine the resource string (ie the item's path in S3, including the bucket name)
View Full Code Here

        String bucketName = ""; // Root path of S3 service lists the user's buckets.
        HttpMethodBase httpMethod =  performRestGet(bucketName, null, null, null);
        String contentType = httpMethod.getResponseHeader("Content-Type").getValue();
           
        if (!Mimetypes.MIMETYPE_XML.equals(contentType)) {
            throw new S3ServiceException("Expected XML document response from S3 but received content type " +
                contentType);
        }

        S3Bucket[] buckets = (new XmlResponsesSaxParser()).parseListMyBucketsResponse(
            new HttpMethodReleaseInputStream(httpMethod)).getBuckets();
View Full Code Here

            String aclAsXml = acl.toXml();
            metadata.put("Content-Length", String.valueOf(aclAsXml.length()));
            performRestPut(bucketName, objectKey, metadata, requestParameters,
                new StringRequestEntity(aclAsXml, "text/plain", Constants.DEFAULT_ENCODING));
        } catch (UnsupportedEncodingException e) {
            throw new S3ServiceException("Unable to encode ACL XML document", e);
        }
    }
View Full Code Here

            try {
                CreateBucketConfiguration config = new CreateBucketConfiguration(location);
                metadata.put("Content-Length", String.valueOf(config.toXml().length()));
                requestEntity = new StringRequestEntity(config.toXml(), "text/xml", Constants.DEFAULT_ENCODING);               
            } catch (UnsupportedEncodingException e) {
                throw new S3ServiceException("Unable to encode CreateBucketConfiguration XML document", e);
            }   
        }
       
        Map map = createObjectImpl(bucketName, null, null, requestEntity, metadata, acl);
       
View Full Code Here

            String statusAsXml = status.toXml();
            metadata.put("Content-Length", String.valueOf(statusAsXml.length()));
            performRestPut(bucketName, null, metadata, requestParameters,
                new StringRequestEntity(statusAsXml, "text/plain", Constants.DEFAULT_ENCODING));               
        } catch (UnsupportedEncodingException e) {
            throw new S3ServiceException("Unable to encode LoggingStatus XML document", e);
        }   
    }
View Full Code Here

                object.closeDataInputStream();
            } catch (IOException e) {
                log.warn("Unable to close data input stream for object '" + object.getKey() + "'", e);
            }
        } catch (URIException e) {
            throw new S3ServiceException("Unable to lookup URI for object created with signed PUT", e);
        } catch (UnsupportedEncodingException e) {
            throw new S3ServiceException("Unable to determine name of object created with signed PUT", e);
        }       
        return object;
    }
View Full Code Here

                try {
                    String aclAsXml = acl.toXml();
                    putMethod.setRequestEntity(new StringRequestEntity(
                        aclAsXml, "text/xml", Constants.DEFAULT_ENCODING));
                } catch (UnsupportedEncodingException e) {
                    throw new S3ServiceException("Unable to encode ACL XML document", e);
                }   

            }
        }
       
View Full Code Here

        try {
            responseObject = ServiceUtils.buildObjectFromUrl(
                httpMethod.getURI().getHost(),
                httpMethod.getPath().substring(1));
        } catch (URIException e) {
            throw new S3ServiceException("Unable to lookup URI for object created with signed PUT", e);
        } catch (UnsupportedEncodingException e) {
            throw new S3ServiceException("Unable to determine name of object created with signed PUT", e);
        }
       
        responseObject.replaceAllMetadata(ServiceUtils.cleanRestMetadataMap(map));
        responseObject.setMetadataComplete(true); // Flag this object as having the complete metadata set.
        if (!headOnly) {
View Full Code Here

TOP

Related Classes of org.jets3t.service.S3ServiceException

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.