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

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

Introduction

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

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

From source file:com.kylinolap.jdbc.stub.KylinClient.java

@Override
public void connect() throws ConnectionException {
    PostMethod post = new PostMethod(conn.getConnectUrl());
    HttpClient httpClient = new HttpClient();

    if (conn.getConnectUrl().toLowerCase().startsWith("https://")) {
        registerSsl();//from w  ww  .  j  av a2  s .  c  o  m
    }
    addPostHeaders(post);

    try {
        StringRequestEntity requestEntity = new StringRequestEntity("{}", "application/json", "UTF-8");
        post.setRequestEntity(requestEntity);
        httpClient.executeMethod(post);

        if (post.getStatusCode() != 200 && post.getStatusCode() != 201) {
            logger.error("Authentication Failed with error code " + post.getStatusCode() + " and message:\n"
                    + post.getResponseBodyAsString());

            throw new ConnectionException("Authentication Failed.");
        }
    } catch (HttpException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ConnectionException(e.getLocalizedMessage());
    } catch (IOException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ConnectionException(e.getLocalizedMessage());
    }
}

From source file:com.kylinolap.jdbc.stub.KylinClient.java

@Override
public MetaProject getMetadata(String project) throws ConnectionException {
    GetMethod get = new GetMethod(conn.getMetaProjectUrl(project));
    HttpClient httpClient = new HttpClient();

    if (conn.getConnectUrl().toLowerCase().startsWith("https://")) {
        registerSsl();//www. ja  v  a 2  s .  c  om
    }
    addPostHeaders(get);

    List<TableMetaStub> tableMetaStubs = null;
    try {
        httpClient.executeMethod(get);

        if (get.getStatusCode() != 200 && get.getStatusCode() != 201) {
            logger.error("Authentication Failed with error code " + get.getStatusCode() + " and message:\n"
                    + get.getResponseBodyAsString());

            throw new ConnectionException("Authentication Failed.");
        }

        tableMetaStubs = new ObjectMapper().readValue(get.getResponseBodyAsString(),
                new TypeReference<List<TableMetaStub>>() {
                });

        List<MetaTable> tables = new ArrayList<MetaTable>();
        HashMultimap<String, MetaTable> schemasMap = HashMultimap.create();

        for (TableMetaStub tableMetaStub : tableMetaStubs) {
            List<MetaColumn> columns = new ArrayList<MetaColumn>();

            for (ColumnMetaStub columnMetaStub : tableMetaStub.getColumns()) {
                MetaColumn column = createNewColumn(columnMetaStub);
                columns.add(column);
            }

            MetaTable table = createNewTable(tableMetaStub, columns);
            tables.add(table);
            schemasMap.put(tableMetaStub.getTABLE_CAT() + "#" + tableMetaStub.getTABLE_SCHEM(), table);
        }

        HashMultimap<String, MetaSchema> catalogMap = HashMultimap.create();
        List<MetaSchema> schemas = new ArrayList<MetaSchema>();
        for (String key : schemasMap.keySet()) {
            String cat = key.split("#")[0];
            String schema = key.split("#")[1];
            MetaSchema metaSchema = new MetaSchema(cat, schema, new ArrayList<MetaTable>(schemasMap.get(key)));
            schemas.add(metaSchema);
            catalogMap.put(cat, metaSchema);
        }

        List<MetaCatalog> catalogs = new ArrayList<MetaCatalog>();
        for (String key : catalogMap.keySet()) {
            MetaCatalog metaCatalog = new MetaCatalog(key, new ArrayList<MetaSchema>(catalogMap.get(key)));
            catalogs.add(metaCatalog);
        }

        return new MetaProject(project, catalogs);
    } catch (HttpException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ConnectionException(e.getLocalizedMessage());
    } catch (IOException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ConnectionException(e.getLocalizedMessage());
    }
}

From source file:com.autentia.mvn.plugin.changes.HttpRequest.java

/**
 * Send a GET method request to the given link using the configured HttpClient, possibly following redirects, and returns
 * the response as String.//from   w  ww .ja va 2s  . co  m
 * 
 * @param cl the HttpClient
 * @param link the URL
 * @throws HttpStatusException
 * @throws IOException
 */
public byte[] sendGetRequest(final HttpClient cl, final String link) throws HttpStatusException, IOException {
    try {
        final GetMethod gm = new GetMethod(link);

        this.getLog().info("Downloading from Bugzilla at: " + link);

        gm.setFollowRedirects(true);

        cl.executeMethod(gm);

        final StatusLine sl = gm.getStatusLine();

        if (sl == null) {
            this.getLog().error("Unknown error validating link: " + link);

            throw new HttpStatusException("UNKNOWN STATUS");
        }

        // if we get a redirect, do so
        if (gm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
            final Header locationHeader = gm.getResponseHeader("Location");

            if (locationHeader == null) {
                this.getLog().warn("Site sent redirect, but did not set Location header");
            } else {
                final String newLink = locationHeader.getValue();

                this.getLog().debug("Following redirect to " + newLink);

                this.sendGetRequest(cl, newLink);
            }
        }

        if (gm.getStatusCode() == HttpStatus.SC_OK) {
            final InputStream is = gm.getResponseBodyAsStream();
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            final byte[] buff = new byte[256];
            int readed = is.read(buff);
            while (readed != -1) {
                baos.write(buff, 0, readed);
                readed = is.read(buff);
            }
            this.getLog().debug("Downloading from Bugzilla was successful");
            return baos.toByteArray();
        } else {
            this.getLog().warn("Downloading from Bugzilla failed. Received: [" + gm.getStatusCode() + "]");
            throw new HttpStatusException("WRONG STATUS");
        }
    } catch (final HttpException e) {
        if (this.getLog().isDebugEnabled()) {
            this.getLog().error("Error downloading issues from Bugzilla:", e);
        } else {
            this.getLog().error("Error downloading issues from Bugzilla url: " + e.getLocalizedMessage());

        }
        throw e;
    } catch (final IOException e) {
        if (this.getLog().isDebugEnabled()) {
            this.getLog().error("Error downloading issues from Bugzilla:", e);
        } else {
            this.getLog().error("Error downloading issues from Bugzilla. Cause is " + e.getLocalizedMessage());
        }
        throw e;
    }
}

From source file:com.autentia.mvn.plugin.changes.HttpRequest.java

/**
 * Send a GET method request to the given link using the configured HttpClient, possibly following redirects, and returns
 * the response as String.//from w w  w .  j  a v a  2 s  .c  o m
 * 
 * @param cl the HttpClient
 * @param link the URL
 * @throws HttpStatusException
 * @throws IOException
 */
public byte[] sendPostRequest(final HttpClient cl, final String link, final String parameters)
        throws HttpStatusException, IOException {
    try {
        final PostMethod pm = new PostMethod(link);

        if (parameters != null) {
            final String[] params = parameters.split("&");
            for (final String param : params) {
                final String[] pair = param.split("=");
                if (pair.length == 2) {
                    pm.addParameter(pair[0], pair[1]);
                }
            }
        }

        this.getLog().info("Downloading from Bugzilla at: " + link);

        cl.executeMethod(pm);

        final StatusLine sl = pm.getStatusLine();

        if (sl == null) {
            this.getLog().error("Unknown error validating link: " + link);

            throw new HttpStatusException("UNKNOWN STATUS");
        }

        // if we get a redirect, throws exception
        if (pm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
            this.getLog().debug("Attempt to redirect ");
            throw new HttpStatusException("Attempt to redirect");
        }

        if (pm.getStatusCode() == HttpStatus.SC_OK) {
            final InputStream is = pm.getResponseBodyAsStream();
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            final byte[] buff = new byte[256];
            int readed = is.read(buff);
            while (readed != -1) {
                baos.write(buff, 0, readed);
                readed = is.read(buff);
            }

            this.getLog().debug("Downloading from Bugzilla was successful");
            return baos.toByteArray();
        } else {
            this.getLog().warn("Downloading from Bugzilla failed. Received: [" + pm.getStatusCode() + "]");
            throw new HttpStatusException("WRONG STATUS");
        }
    } catch (final HttpException e) {
        if (this.getLog().isDebugEnabled()) {
            this.getLog().error("Error downloading issues from Bugzilla:", e);
        } else {
            this.getLog().error("Error downloading issues from Bugzilla url: " + e.getLocalizedMessage());

        }
        throw e;
    } catch (final IOException e) {
        if (this.getLog().isDebugEnabled()) {
            this.getLog().error("Error downloading issues from Bugzilla:", e);
        } else {
            this.getLog().error("Error downloading issues from Bugzilla. Cause is " + e.getLocalizedMessage());
        }
        throw e;
    }
}

From source file:org.apache.kylin.jdbc.stub.KylinClient.java

@Override
public void connect() throws ConnectionException {
    PostMethod post = new PostMethod(conn.getConnectUrl());
    HttpClient httpClient = new HttpClient();

    if (conn.getConnectUrl().toLowerCase().startsWith("https://")) {
        registerSsl();//from www.j  av a 2 s.  c o  m
    }
    addPostHeaders(post);

    try {
        StringRequestEntity requestEntity = new StringRequestEntity("{}", "application/json", "UTF-8");
        post.setRequestEntity(requestEntity);
        httpClient.executeMethod(post);

        if (post.getStatusCode() != 200 && post.getStatusCode() != 201) {
            logger.error("Connect failed with error code " + post.getStatusCode() + " and message:\n"
                    + post.getResponseBodyAsString());

            throw new ConnectionException("Connect failed, error code " + post.getStatusCode()
                    + " and message: " + post.getResponseBodyAsString());
        }
    } catch (HttpException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ConnectionException(e.getLocalizedMessage());
    } catch (IOException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ConnectionException(e.getLocalizedMessage());
    }
}

From source file:org.apache.kylin.jdbc.stub.KylinClient.java

@Override
public KylinMetaImpl.MetaProject getMetadata(String project) throws ConnectionException {
    GetMethod get = new GetMethod(conn.getMetaProjectUrl(project));
    HttpClient httpClient = new HttpClient();

    if (conn.getConnectUrl().toLowerCase().startsWith("https://")) {
        registerSsl();//from w  w w. j a  v a  2s .com
    }
    addPostHeaders(get);

    List<TableMetaStub> tableMetaStubs = null;
    try {
        httpClient.executeMethod(get);

        if (get.getStatusCode() != 200 && get.getStatusCode() != 201) {
            logger.error("Connect failed with error code " + get.getStatusCode() + " and message:\n"
                    + get.getResponseBodyAsString());

            throw new ConnectionException("Connect failed, error code " + get.getStatusCode() + " and message: "
                    + get.getResponseBodyAsString());
        }

        tableMetaStubs = new ObjectMapper().readValue(get.getResponseBodyAsString(),
                new TypeReference<List<TableMetaStub>>() {
                });

        List<KylinMetaImpl.MetaTable> tables = new ArrayList<KylinMetaImpl.MetaTable>();
        HashMultimap<String, KylinMetaImpl.MetaTable> schemasMap = HashMultimap.create();

        for (TableMetaStub tableMetaStub : tableMetaStubs) {
            List<KylinMetaImpl.MetaColumn> columns = new ArrayList<KylinMetaImpl.MetaColumn>();

            for (ColumnMetaStub columnMetaStub : tableMetaStub.getColumns()) {
                KylinMetaImpl.MetaColumn column = createNewColumn(columnMetaStub);
                columns.add(column);
            }

            KylinMetaImpl.MetaTable table = createNewTable(tableMetaStub, columns);
            tables.add(table);
            schemasMap.put(tableMetaStub.getTABLE_CAT() + "#" + tableMetaStub.getTABLE_SCHEM(), table);
        }

        HashMultimap<String, KylinMetaImpl.MetaSchema> catalogMap = HashMultimap.create();
        List<KylinMetaImpl.MetaSchema> schemas = new ArrayList<KylinMetaImpl.MetaSchema>();
        for (String key : schemasMap.keySet()) {
            String cat = key.split("#")[0];
            String schema = key.split("#")[1];
            KylinMetaImpl.MetaSchema metaSchema = new KylinMetaImpl.MetaSchema(cat, schema,
                    new ArrayList<KylinMetaImpl.MetaTable>(schemasMap.get(key)));
            schemas.add(metaSchema);
            catalogMap.put(cat, metaSchema);
        }

        List<KylinMetaImpl.MetaCatalog> catalogs = new ArrayList<KylinMetaImpl.MetaCatalog>();
        for (String key : catalogMap.keySet()) {
            KylinMetaImpl.MetaCatalog metaCatalog = new KylinMetaImpl.MetaCatalog(key,
                    new ArrayList<KylinMetaImpl.MetaSchema>(catalogMap.get(key)));
            catalogs.add(metaCatalog);
        }

        return new KylinMetaImpl.MetaProject(project, catalogs);
    } catch (HttpException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ConnectionException(e.getLocalizedMessage());
    } catch (IOException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ConnectionException(e.getLocalizedMessage());
    }
}

From source file:org.apache.maven.plugin.jira.AbstractJiraDownloader.java

/**
 * Downloads the given link using the configured HttpClient, possibly following redirects.
 *
 * @param cl     the HttpClient/*w w w. j  a  v a2 s.  com*/
 * @param link   the URL to JIRA
 */
private void download(final HttpClient cl, final String link) {
    try {
        GetMethod gm = new GetMethod(link);

        getLog().info("Downloading from JIRA at: " + link);

        gm.setFollowRedirects(true);

        cl.executeMethod(gm);

        StatusLine sl = gm.getStatusLine();

        if (sl == null) {
            getLog().error("Unknown error validating link: " + link);

            return;
        }

        // if we get a redirect, do so
        if (gm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header locationHeader = gm.getResponseHeader("Location");

            if (locationHeader == null) {
                getLog().warn("Site sent redirect, but did not set Location header");
            } else {
                String newLink = locationHeader.getValue();

                getLog().debug("Following redirect to " + newLink);

                download(cl, newLink);
            }
        }

        if (gm.getStatusCode() == HttpStatus.SC_OK) {
            final InputStream responseBodyStream = gm.getResponseBodyAsStream();

            if (!output.getParentFile().exists()) {
                output.getParentFile().mkdirs();
            }

            // write the response to file
            OutputStream out = null;
            try {
                out = new FileOutputStream(output);
                IOUtil.copy(responseBodyStream, out);
            } finally {
                IOUtil.close(out);
                IOUtil.close(responseBodyStream);
            }

            getLog().debug("Downloading from JIRA was successful");
        } else {
            getLog().warn("Downloading from JIRA failed. Received: [" + gm.getStatusCode() + "]");
        }
    } catch (HttpException e) {
        if (getLog().isDebugEnabled()) {
            getLog().error("Error downloading issues from JIRA:", e);
        } else {
            getLog().error("Error downloading issues from JIRA url: " + e.getLocalizedMessage());

        }
    } catch (IOException e) {
        if (getLog().isDebugEnabled()) {
            getLog().error("Error downloading issues from JIRA:", e);
        } else {
            getLog().error("Error downloading issues from JIRA. Cause is " + e.getLocalizedMessage());
        }
    }
}