Example usage for org.apache.commons.httpclient HttpException HttpException

List of usage examples for org.apache.commons.httpclient HttpException HttpException

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpException HttpException.

Prototype

public HttpException(String message) 

Source Link

Document

Creates a new HttpException with the specified detail message.

Usage

From source file:com.motorola.studio.android.localization.translators.GoogleTranslator.java

private static void checkStatusCode(int statusCode, String response) throws HttpException {
    switch (statusCode) {
    case HttpStatus.SC_OK:
        //do nothing
        break;/*w w  w . j av a  2 s  .  c om*/
    case HttpStatus.SC_BAD_REQUEST:
        throw new HttpException(NLS.bind(TranslateNLS.GoogleTranslator_ErrorMessageExecutingRequest,
                getErrorMessage(response)));

    case HttpStatus.SC_REQUEST_URI_TOO_LONG:
        throw new HttpException(TranslateNLS.GoogleTranslator_Error_QueryTooBig);

    case HttpStatus.SC_FORBIDDEN:
        throw new HttpException(NLS.bind(TranslateNLS.GoogleTranslator_ErrorMessageNoValidTranslationReturned,
                getErrorMessage(response)));

    default:
        throw new HttpException(NLS.bind(TranslateNLS.GoogleTranslator_Error_HTTPRequestError,
                new Object[] { statusCode, getErrorMessage(response) }));

    }
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Build Http Exception from methode status
 *
 * @param method Http Method/*from ww w .j a v a 2s. co  m*/
 * @return Http Exception
 */
public static HttpException buildHttpException(HttpMethod method) {
    int status = method.getStatusCode();
    StringBuilder message = new StringBuilder();
    message.append(status).append(' ').append(method.getStatusText());
    try {
        message.append(" at ").append(method.getURI().getURI());
        if (method instanceof CopyMethod || method instanceof MoveMethod) {
            message.append(" to ").append(method.getRequestHeader("Destination"));
        }
    } catch (URIException e) {
        message.append(method.getPath());
    }
    // 440 means forbidden on Exchange
    if (status == 440) {
        return new LoginTimeoutException(message.toString());
    } else if (status == HttpStatus.SC_FORBIDDEN) {
        return new HttpForbiddenException(message.toString());
    } else if (status == HttpStatus.SC_NOT_FOUND) {
        return new HttpNotFoundException(message.toString());
    } else if (status == HttpStatus.SC_PRECONDITION_FAILED) {
        return new HttpPreconditionFailedException(message.toString());
    } else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
        return new HttpServerErrorException(message.toString());
    } else {
        return new HttpException(message.toString());
    }
}

From source file:edu.umd.cs.eclipse.courseProjectManager.TurninProjectAction.java

private static InputStream getSubmitUserFileFromServer(String loginName, String password,
        Properties allProperties) throws IOException, HttpException {
    String url = allProperties.getProperty("baseURL");
    url += "/eclipse/NegotiateOneTimePassword";
    // System.out.println(url);
    // Debug.print("url: " +url);
    PostMethod post = new PostMethod(url);
    post.addParameter("loginName", loginName);
    post.addParameter("password", password);

    addParameter(post, "courseKey", allProperties);
    addParameter(post, "projectNumber", allProperties);
    post.addParameter("submitClientVersion", AutoCVSPlugin.getPlugin().getVersion());

    HttpClient client = new HttpClient();
    client.setConnectionTimeout(5000);//from   w w w .  j  a  v a 2  s  .co  m

    // System.out.println("Preparing to execute method");
    int status = client.executeMethod(post);
    // System.out.println("Post finished with status: " +status);

    if (status != HttpStatus.SC_OK) {
        throw new HttpException(
                "Unable to negotiate one-time password with the server: " + post.getResponseBodyAsString());
    }

    return post.getResponseBodyAsStream();
}

From source file:org.apache.ambari.view.slider.rest.client.BaseHttpClient.java

@SuppressWarnings("deprecation")
public JsonElement doGetJson(String url, String path) throws HttpException, IOException {
    GetMethod get = new GetMethod(url + path);
    if (isNeedsAuthentication()) {
        get.setDoAuthentication(true);//from  ww w .j a va2  s .  c om
    }
    int executeMethod = getHttpClient().executeMethod(get);
    switch (executeMethod) {
    case HttpStatus.SC_OK:
        JsonElement jsonElement = new JsonParser()
                .parse(new JsonReader(new InputStreamReader(get.getResponseBodyAsStream())));
        return jsonElement;
    case HttpStatus.SC_NOT_FOUND:
        return null;
    default:
        HttpException httpException = new HttpException(get.getResponseBodyAsString());
        httpException.setReason(HttpStatus.getStatusText(executeMethod));
        httpException.setReasonCode(executeMethod);
        throw httpException;
    }
}

From source file:org.apache.asterix.experiment.action.derived.RunSQLPPFileAction.java

@Override
public void doPerform() throws Exception {
    FileOutputStream csvFileOut = new FileOutputStream(csvFilePath.toFile());
    PrintWriter printer = new PrintWriter(csvFileOut, true);
    try {/*from  www  . j a  v a2  s.  co m*/
        if (aqlFilePath.toFile().isDirectory()) {
            for (File f : aqlFilePath.toFile().listFiles()) {
                queriesToRun.add(f.toPath());
            }
        } else {
            queriesToRun.add(aqlFilePath);
        }

        for (Path p : queriesToRun) {
            String sqlpp = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(Files.readAllBytes(p))).toString();
            String uri = MessageFormat.format(REST_URI_TEMPLATE, restHost, String.valueOf(restPort));
            HttpPost post = new HttpPost(uri);
            post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
            post.setEntity(new StringEntity(sqlpp, StandardCharsets.UTF_8));
            long start = System.currentTimeMillis();
            HttpResponse resp = httpClient.execute(post);
            HttpEntity entity = resp.getEntity();
            if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                throw new HttpException("Query returned error" + EntityUtils.toString(entity));
            }
            EntityUtils.consume(entity);
            long end = System.currentTimeMillis();
            long wallClock = end - start;
            String currLine = p.getFileName().toString() + ',' + wallClock;
            System.out.println(currLine);
            printer.print(currLine + '\n');
        }
    } finally {
        printer.close();
    }
}

From source file:org.apache.cocoon.components.repository.impl.WebDAVRepository.java

public InputStream getContentStream(String uri) throws ProcessingException {

    if (this.getLogger().isDebugEnabled()) {
        this.getLogger().debug("getting content of: " + uri);
    }//from  w  ww  . j  a v a2  s  .co  m

    try {

        WebdavResource resource = WebDAVUtil.getWebdavResource(this.getAbsoluteURI(uri));
        if (!resource.exists()) {
            throw new HttpException(uri + " does not exist");
        }
        return new BufferedInputStream(resource.getMethodData());

    } catch (MalformedURLException mue) {
        throw new ProcessingException("Bad URL for resource: " + this.repoBaseUrl + uri, mue);
    } catch (IOException ioe) {
        throw new ProcessingException("Error loading resource: " + this.repoBaseUrl + uri, ioe);
    }
}

From source file:org.apache.cocoon.components.source.impl.WebDAVSource.java

/**
 * Return an <code>InputStream</code> object to read from the source.
 * This is the data at the point of invocation of this method,
 * so if this is Modifiable, you might get different content
 * from two different invocations.//from www.ja  v  a  2 s.com
 */
public InputStream getInputStream() throws IOException, SourceException {
    initResource(WebdavResource.BASIC, DepthSupport.DEPTH_0);
    try {
        if (this.resource.isCollection()) {
            // [UH] FIXME: why list collection as XML here?
            // I think its a concern for the TraversableGenerator.
            WebdavResource[] resources = this.resource.listWebdavResources();
            return resourcesToXml(resources);
        } else {
            BufferedInputStream bi = null;
            bi = new BufferedInputStream(this.resource.getMethodData());
            if (!this.resource.exists()) {
                throw new HttpException(getSecureURI() + " does not exist");
            }
            return bi;
        }
    } catch (HttpException he) {
        throw new SourceException("Could not get WebDAV resource " + getSecureURI(), he);
    } catch (Exception e) {
        throw new SourceException("Could not get WebDAV resource" + getSecureURI(), e);
    }
}

From source file:org.apache.cocoon.components.webdav.WebDAVUtil.java

/**
 * create a new resource on the server //w  w  w  . j  ava 2s.  com
 * 
 * @param uri  the uri of the resource.
 * @param content  the content to initialize the resource with.
 * @throws HttpException
 * @throws IOException
 */
static public void createResource(final String uri, final String content) throws HttpException, IOException {

    final String filename = uri.substring(uri.lastIndexOf("/"));
    final String uriPrefix = uri.substring(0, uri.lastIndexOf("/") + 1);
    final HttpURL sourceURL = new HttpURL(uri);
    final WebdavResource resource = getWebdavResource(uriPrefix);

    if (!resource.putMethod(uriPrefix + filename, content)) {
        throw new HttpException("Error creating resource: " + uri + " Status: " + resource.getStatusCode()
                + " Message: " + resource.getStatusMessage());
    }
}

From source file:org.apache.cocoon.components.webdav.WebDAVUtil.java

/**
 * copy a WebDAV resource /*from  www .ja v  a  2  s. com*/
 * 
 * @param from  the URI of the resource to copy
 * @param to  the URI of the destination
 * @param overwrite  if true overwrites the destination
 * @param recurse  if true recursively creates parent collections if not existant
 * @throws HttpException
 * @throws IOException
 */
static public void copyResource(String from, String to, boolean recurse, boolean overwrite)
        throws HttpException, IOException {

    String relativeDestination = (to.substring(to.indexOf("://") + 3));
    relativeDestination = relativeDestination.substring(relativeDestination.indexOf("/"));

    // make parentCollection of target if not existant
    if (recurse)
        WebDAVUtil.makePath(to.substring(0, to.lastIndexOf("/")));

    // copy the resource
    WebdavResource resource = WebDAVUtil.getWebdavResource(from);
    resource.setOverwrite(overwrite);
    if (!resource.copyMethod(relativeDestination)) {
        throw new HttpException("Error copying resource: " + from + " Status: " + resource.getStatusCode()
                + " Message: " + resource.getStatusMessage());
    }
}

From source file:org.apache.cocoon.components.webdav.WebDAVUtil.java

/**
 * move a WebDAV resource /*from  w ww .ja va2 s.c o  m*/
 * 
 * @param from  the URI of the resource to move
 * @param to  the URI of the destination
 * @param overwrite  if true overwrites the destination
 * @param recurse  if true recursively creates parent collections if not existant
 * @throws HttpException
 * @throws IOException
 */
static public void moveResource(String from, String to, boolean recurse, boolean overwrite)
        throws HttpException, IOException {

    String relativeDestination = (to.substring(to.indexOf("://") + 3));
    relativeDestination = relativeDestination.substring(relativeDestination.indexOf("/"));

    // make parentCollection if not existant
    if (recurse)
        WebDAVUtil.makePath(to.substring(0, to.lastIndexOf("/")));

    // move the resource
    WebdavResource resource = WebDAVUtil.getWebdavResource(from);
    resource.setOverwrite(overwrite);
    if (!resource.moveMethod(relativeDestination)) {
        throw new HttpException("Error moving resource: " + from + " Status: " + resource.getStatusCode()
                + " Message: " + resource.getStatusMessage());
    }
}