Search in sources :

Example 26 with HttpException

use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.

the class RegionsApiUtil method makeDomainAPICall.

/**
     * Makes an api call using region service end_point, api command and params
     * Returns Domain object on success
     * @param region
     * @param command
     * @param params
     * @return
     */
protected static RegionDomain makeDomainAPICall(Region region, String command, List<NameValuePair> params) {
    try {
        String url = buildUrl(buildParams(command, params), region);
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(url);
        if (client.executeMethod(method) == 200) {
            InputStream is = method.getResponseBodyAsStream();
            XStream xstream = new XStream(new DomDriver());
            //Translate response to Domain object
            xstream.alias("domain", RegionDomain.class);
            xstream.aliasField("id", RegionDomain.class, "uuid");
            xstream.aliasField("parentdomainid", RegionDomain.class, "parentUuid");
            xstream.aliasField("networkdomain", DomainVO.class, "networkDomain");
            try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
                return (RegionDomain) in.readObject();
            } catch (IOException e) {
                s_logger.error(e.getMessage());
                return null;
            }
        } else {
            return null;
        }
    } catch (HttpException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (IOException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (ClassNotFoundException e) {
        s_logger.error(e.getMessage());
        return null;
    }
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod) ObjectInputStream(java.io.ObjectInputStream)

Example 27 with HttpException

use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.

the class RegionsApiUtil method makeUserAccountAPICall.

/**
     * Makes an api call using region service end_point, api command and params
     * Returns UserAccount object on success
     * @param region
     * @param command
     * @param params
     * @return
     */
protected static UserAccount makeUserAccountAPICall(Region region, String command, List<NameValuePair> params) {
    try {
        String url = buildUrl(buildParams(command, params), region);
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(url);
        if (client.executeMethod(method) == 200) {
            InputStream is = method.getResponseBodyAsStream();
            XStream xstream = new XStream(new DomDriver());
            xstream.alias("useraccount", UserAccountVO.class);
            xstream.aliasField("id", UserAccountVO.class, "uuid");
            try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
                return (UserAccountVO) in.readObject();
            } catch (IOException e) {
                s_logger.error(e.getMessage());
                return null;
            }
        } else {
            return null;
        }
    } catch (HttpException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (IOException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (ClassNotFoundException e) {
        s_logger.error(e.getMessage());
        return null;
    }
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) UserAccountVO(com.cloud.user.UserAccountVO) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod) ObjectInputStream(java.io.ObjectInputStream)

Example 28 with HttpException

use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.

the class RegionsApiUtil method makeAccountAPICall.

/**
     * Makes an api call using region service end_point, api command and params
     * Returns Account object on success
     * @param region
     * @param command
     * @param params
     * @return
     */
protected static RegionAccount makeAccountAPICall(Region region, String command, List<NameValuePair> params) {
    try {
        String url = buildUrl(buildParams(command, params), region);
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(url);
        if (client.executeMethod(method) == 200) {
            InputStream is = method.getResponseBodyAsStream();
            //Translate response to Account object
            XStream xstream = new XStream(new DomDriver());
            xstream.alias("account", RegionAccount.class);
            xstream.alias("user", RegionUser.class);
            xstream.aliasField("id", RegionAccount.class, "uuid");
            xstream.aliasField("name", RegionAccount.class, "accountName");
            xstream.aliasField("accounttype", RegionAccount.class, "type");
            xstream.aliasField("domainid", RegionAccount.class, "domainUuid");
            xstream.aliasField("networkdomain", RegionAccount.class, "networkDomain");
            xstream.aliasField("id", RegionUser.class, "uuid");
            xstream.aliasField("accountId", RegionUser.class, "accountUuid");
            try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
                return (RegionAccount) in.readObject();
            } catch (IOException e) {
                s_logger.error(e.getMessage());
                return null;
            }
        } else {
            return null;
        }
    } catch (HttpException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (IOException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (ClassNotFoundException e) {
        s_logger.error(e.getMessage());
        return null;
    }
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod) ObjectInputStream(java.io.ObjectInputStream)

Example 29 with HttpException

use of org.apache.commons.httpclient.HttpException in project maven-plugins by apache.

the class ClassicJiraDownloader method download.

/**
     * Downloads the given link using the configured HttpClient, possibly following redirects.
     *
     * @param cl the HttpClient
     * @param link the URL to JIRA
     */
private void download(final HttpClient cl, final String link) {
    InputStream in = null;
    OutputStream out = null;
    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) {
            in = gm.getResponseBodyAsStream();
            if (!output.getParentFile().exists()) {
                output.getParentFile().mkdirs();
            }
            // write the response to file
            out = new FileOutputStream(output);
            IOUtil.copy(in, out);
            out.close();
            out = null;
            in.close();
            in = null;
            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());
        }
    } finally {
        IOUtil.close(out);
        IOUtil.close(in);
    }
}
Also used : StatusLine(org.apache.commons.httpclient.StatusLine) Header(org.apache.commons.httpclient.Header) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException)

Example 30 with HttpException

use of org.apache.commons.httpclient.HttpException in project sling by apache.

the class FsMountHelper method getCurrentConfigurations.

/**
     * Return all file provider configs for this project
     * @param targetUrl The targetUrl of the webconsole
     * @return A map (may be empty) with the pids as keys and the configurations as values
     * @throws MojoExecutionException
     */
public Map<String, FsResourceConfiguration> getCurrentConfigurations(final String targetUrl) throws MojoExecutionException {
    log.debug("Getting current file provider configurations.");
    final Map<String, FsResourceConfiguration> result = new HashMap<>();
    final String getUrl = targetUrl + "/configMgr/(service.factoryPid=" + FS_FACTORY + ").json";
    final GetMethod get = new GetMethod(getUrl);
    try {
        final int status = httpClient.executeMethod(get);
        if (status == 200) {
            String contentType = get.getResponseHeader(HEADER_CONTENT_TYPE).getValue();
            int pos = contentType.indexOf(';');
            if (pos != -1) {
                contentType = contentType.substring(0, pos);
            }
            if (!JsonSupport.JSON_MIME_TYPE.equals(contentType)) {
                log.debug("Response type from web console is not JSON, but " + contentType);
                throw new MojoExecutionException("The Apache Felix Web Console is too old to mount " + "the initial content through file system provider configs. " + "Either upgrade the web console or disable this feature.");
            }
            final String jsonText;
            try (InputStream jsonResponse = get.getResponseBodyAsStream()) {
                jsonText = IOUtils.toString(jsonResponse, CharEncoding.UTF_8);
            }
            try {
                JsonArray array = JsonSupport.parseArray(jsonText);
                for (int i = 0; i < array.size(); i++) {
                    final JsonObject obj = array.getJsonObject(i);
                    final String pid = obj.getString("pid");
                    final JsonObject properties = obj.getJsonObject("properties");
                    final String fsmode = getConfigPropertyValue(properties, PROPERTY_FSMODE);
                    final String path = getConfigPropertyValue(properties, PROPERTY_PATH);
                    final String initialContentImportOptions = getConfigPropertyValue(properties, PROPERTY_INITIAL_CONTENT_IMPORT_OPTIONS);
                    final String fileVaultFilterXml = getConfigPropertyValue(properties, PROPERTY_FILEVAULT_FILTER_XML);
                    String root = getConfigPropertyValue(properties, PROPERTY_ROOTS);
                    if (root == null) {
                        root = getConfigPropertyValue(properties, PROPERTY_ROOT);
                    }
                    if (path != null && path.startsWith(this.project.getBasedir().getAbsolutePath()) && root != null) {
                        FsResourceConfiguration cfg = new FsResourceConfiguration().fsMode(fsmode).providerRootPath(path).contentRootDir(root).initialContentImportOptions(initialContentImportOptions).fileVaultFilterXml(fileVaultFilterXml);
                        log.debug("Found configuration with pid: " + pid + ", " + cfg);
                        result.put(pid, cfg);
                    }
                }
            } catch (JsonException ex) {
                throw new MojoExecutionException("Reading configuration from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
            }
        }
    } catch (HttpException ex) {
        throw new MojoExecutionException("Reading configuration from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new MojoExecutionException("Reading configuration from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
    } finally {
        get.releaseConnection();
    }
    return result;
}
Also used : JsonException(javax.json.JsonException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) JsonObject(javax.json.JsonObject) IOException(java.io.IOException) JsonArray(javax.json.JsonArray) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException)

Aggregations

HttpException (org.apache.commons.httpclient.HttpException)61 IOException (java.io.IOException)55 HttpClient (org.apache.commons.httpclient.HttpClient)34 GetMethod (org.apache.commons.httpclient.methods.GetMethod)31 HttpMethod (org.apache.commons.httpclient.HttpMethod)22 InputStream (java.io.InputStream)15 Header (org.apache.commons.httpclient.Header)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)12 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)9 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)8 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)6 Test (org.junit.Test)5 ServiceException (com.zimbra.common.service.ServiceException)4 Server (com.zimbra.cs.account.Server)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 XStream (com.thoughtworks.xstream.XStream)3