Search in sources :

Example 1 with SwiftException

use of org.apache.hadoop.fs.swift.exceptions.SwiftException in project hadoop by apache.

the class SwiftRestClient method pathToObjectLocation.

/**
   * Create the URI needed to query the location of an object
   * @param path object path to retrieve information about
   * @return the URI for the location operation
   * @throws SwiftException if the URI could not be constructed
   */
private URI pathToObjectLocation(SwiftObjectPath path) throws SwiftException {
    URI uri;
    String dataLocationURI = objectLocationURI.toString();
    try {
        if (path.toString().startsWith("/")) {
            dataLocationURI = dataLocationURI.concat(path.toUriPath());
        } else {
            dataLocationURI = dataLocationURI.concat("/").concat(path.toUriPath());
        }
        uri = new URI(dataLocationURI);
    } catch (URISyntaxException e) {
        throw new SwiftException(e);
    }
    return uri;
}
Also used : SwiftException(org.apache.hadoop.fs.swift.exceptions.SwiftException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 2 with SwiftException

use of org.apache.hadoop.fs.swift.exceptions.SwiftException in project hadoop by apache.

the class SwiftRestClient method findObjects.

/**
   * Find objects in a location
   * @param location URI
   * @param requestHeaders optional request headers
   * @return the body of te response
   * @throws IOException IO problems
   */
private byte[] findObjects(String location, final Header[] requestHeaders) throws IOException {
    URI uri;
    preRemoteCommand("findObjects");
    try {
        uri = new URI(location);
    } catch (URISyntaxException e) {
        throw new SwiftException("Bad URI: " + location, e);
    }
    return perform("findObjects", uri, new GetMethodProcessor<byte[]>() {

        @Override
        public byte[] extractResult(GetMethod method) throws IOException {
            if (method.getStatusCode() == SC_NOT_FOUND) {
                //no result
                throw new FileNotFoundException("Not found " + method.getURI());
            }
            return method.getResponseBody();
        }

        @Override
        protected int[] getAllowedStatusCodes() {
            return new int[] { SC_OK, SC_NOT_FOUND };
        }

        @Override
        protected void setup(GetMethod method) throws SwiftInternalStateException {
            setHeaders(method, requestHeaders);
        }
    });
}
Also used : SwiftInternalStateException(org.apache.hadoop.fs.swift.exceptions.SwiftInternalStateException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) FileNotFoundException(java.io.FileNotFoundException) SwiftException(org.apache.hadoop.fs.swift.exceptions.SwiftException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI)

Example 3 with SwiftException

use of org.apache.hadoop.fs.swift.exceptions.SwiftException in project hadoop by apache.

the class SwiftRestClient method encodeUrl.

/**
   * Encode the URL. This extends {@link URLEncoder#encode(String, String)}
   * with a replacement of + with %20.
   * @param url URL string
   * @return an encoded string
   * @throws SwiftException if the URL cannot be encoded
   */
private static String encodeUrl(String url) throws SwiftException {
    if (url.matches(".*\\s+.*")) {
        try {
            url = URLEncoder.encode(url, "UTF-8");
            url = url.replace("+", "%20");
        } catch (UnsupportedEncodingException e) {
            throw new SwiftException("failed to encode URI", e);
        }
    }
    return url;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) SwiftException(org.apache.hadoop.fs.swift.exceptions.SwiftException)

Example 4 with SwiftException

use of org.apache.hadoop.fs.swift.exceptions.SwiftException in project hadoop by apache.

the class SwiftNativeFileSystemStore method getObjectMetadata.

/**
   * Get the metadata of an object
   *
   * @param path path
   * @param newest flag to say "set the newest header", otherwise take any entry
   * @return file metadata. -or null if no headers were received back from the server.
   * @throws IOException           on a problem
   * @throws FileNotFoundException if there is nothing at the end
   */
public SwiftFileStatus getObjectMetadata(Path path, boolean newest) throws IOException, FileNotFoundException {
    SwiftObjectPath objectPath = toObjectPath(path);
    final Header[] headers = stat(objectPath, newest);
    //no headers is treated as a missing file
    if (headers.length == 0) {
        throw new FileNotFoundException("Not Found " + path.toUri());
    }
    boolean isDir = false;
    long length = 0;
    long lastModified = 0;
    for (Header header : headers) {
        String headerName = header.getName();
        if (headerName.equals(SwiftProtocolConstants.X_CONTAINER_OBJECT_COUNT) || headerName.equals(SwiftProtocolConstants.X_CONTAINER_BYTES_USED)) {
            length = 0;
            isDir = true;
        }
        if (SwiftProtocolConstants.HEADER_CONTENT_LENGTH.equals(headerName)) {
            length = Long.parseLong(header.getValue());
        }
        if (SwiftProtocolConstants.HEADER_LAST_MODIFIED.equals(headerName)) {
            final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
            try {
                lastModified = simpleDateFormat.parse(header.getValue()).getTime();
            } catch (ParseException e) {
                throw new SwiftException("Failed to parse " + header.toString(), e);
            }
        }
    }
    if (lastModified == 0) {
        lastModified = System.currentTimeMillis();
    }
    Path correctSwiftPath = getCorrectSwiftPath(path);
    return new SwiftFileStatus(length, isDir, 1, getBlocksize(), lastModified, correctSwiftPath);
}
Also used : Path(org.apache.hadoop.fs.Path) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) Header(org.apache.commons.httpclient.Header) FileNotFoundException(java.io.FileNotFoundException) SwiftException(org.apache.hadoop.fs.swift.exceptions.SwiftException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath)

Example 5 with SwiftException

use of org.apache.hadoop.fs.swift.exceptions.SwiftException in project hadoop by apache.

the class SwiftRestClient method findObjectsByPrefix.

/**
   * Find objects under a prefix
   *
   * @param path path prefix
   * @param requestHeaders optional request headers
   * @return byte[] file data or null if the object was not found
   * @throws IOException on IO Faults
   * @throws FileNotFoundException if nothing is at the end of the URI -that is,
   * the directory is empty
   */
public byte[] findObjectsByPrefix(SwiftObjectPath path, final Header... requestHeaders) throws IOException {
    preRemoteCommand("findObjectsByPrefix");
    URI uri;
    String dataLocationURI = getEndpointURI().toString();
    try {
        String object = path.getObject();
        if (object.startsWith("/")) {
            object = object.substring(1);
        }
        object = encodeUrl(object);
        dataLocationURI = dataLocationURI.concat("/").concat(path.getContainer()).concat("/?prefix=").concat(object);
        uri = new URI(dataLocationURI);
    } catch (URISyntaxException e) {
        throw new SwiftException("Bad URI: " + dataLocationURI, e);
    }
    return perform("findObjectsByPrefix", uri, new GetMethodProcessor<byte[]>() {

        @Override
        public byte[] extractResult(GetMethod method) throws IOException {
            if (method.getStatusCode() == SC_NOT_FOUND) {
                //no result
                throw new FileNotFoundException("Not found " + method.getURI());
            }
            return method.getResponseBody();
        }

        @Override
        protected int[] getAllowedStatusCodes() {
            return new int[] { SC_OK, SC_NOT_FOUND };
        }

        @Override
        protected void setup(GetMethod method) throws SwiftInternalStateException {
            setHeaders(method, requestHeaders);
        }
    });
}
Also used : SwiftInternalStateException(org.apache.hadoop.fs.swift.exceptions.SwiftInternalStateException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) FileNotFoundException(java.io.FileNotFoundException) SwiftException(org.apache.hadoop.fs.swift.exceptions.SwiftException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI)

Aggregations

SwiftException (org.apache.hadoop.fs.swift.exceptions.SwiftException)7 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)2 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 SwiftInternalStateException (org.apache.hadoop.fs.swift.exceptions.SwiftInternalStateException)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Header (org.apache.commons.httpclient.Header)1 Path (org.apache.hadoop.fs.Path)1 SwiftObjectPath (org.apache.hadoop.fs.swift.util.SwiftObjectPath)1