Example usage for org.apache.http.client HttpResponseException getStatusCode

List of usage examples for org.apache.http.client HttpResponseException getStatusCode

Introduction

In this page you can find the example usage for org.apache.http.client HttpResponseException getStatusCode.

Prototype

public int getStatusCode() 

Source Link

Usage

From source file:fr.lissi.belilif.om2m.oao.ContentInstanceManager.java

@Override
public int create(ContentInstance obj) {

    if (obj.getContainer() == null || obj.getContainer().getApplication().getAppId() == null) {
        LOGGER.error(/*from   w  w w .j a v a2  s.c om*/
                "you must specify the parent resource (parent Container) to create a ContentInstanceManager instance.");
        return -1;
    }

    LOGGER.info("Create ContentInstance '" + obj.getValueAsString() + "' for "
            + obj.getContainer().getApplication().getAppId() + "/" + obj.getContainer().getId());
    int resp = -1;
    try {
        resp = WebServiceActions.doPost(
                this.OM2MUrlBase + "applications/" + obj.getContainer().getApplication().getAppId()
                        + "/containers/" + obj.getContainer().getId() + "/contentInstances",
                obj.getValueAsString(), this.headers);

        LOGGER.info("ContentInstance creation in '" + obj.getContainer().getApplication().getAppId() + "/"
                + obj.getContainer().getId() + "' status : " + resp);
    } catch (HttpResponseException e) {
        resp = e.getStatusCode();
        LOGGER.info(e.getStatusCode() + " / " + e.getMessage());
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO ClientProtocolException
        e.printStackTrace();
    } catch (IOException e) {
        // TODO IOException
        e.printStackTrace();
    }
    return resp;
}

From source file:com.splunk.shuttl.archiver.archive.ArchiveRestHandler.java

private void logHttpResponseException(Bucket bucket, HttpResponseException e) {
    logger.error(did("Sent an archive bucket reuqest", "Got non ok http_status",
            "expected HttpStatus.SC_OK or SC_NO_CONTENT", "http_status", e.getStatusCode(), "bucket_name",
            bucket.getName()));//from www.j a  va 2 s . c  o m
}

From source file:me.vertretungsplan.parser.LegionBoardParser.java

private JSONArray getJSONArray(String url) throws IOException, JSONException, CredentialInvalidException {
    try {/*from   w  w w.  j  av  a  2 s  .  c  o m*/
        return new JSONArray(httpGet(url, "UTF-8"));
    } catch (HttpResponseException httpResponseException) {
        if (httpResponseException.getStatusCode() == 404) {
            return null;
        }
        throw httpResponseException;
    }
}

From source file:fr.lissi.belilif.om2m.oao.ContainerManager.java

@Override
public int create(Container obj) {
    // verifier si le container existe avant d'en crer : laisser a au utilisateurs
    // Om2mManager om2mManager = Om2mManagersFactorty.getManager(Om2mManagersFactorty.CONTAINER_MANAGER);
    // OM2MContainerResponse respExist = (OM2MContainerResponse) om2mManager.get(obj);
    // if (respExist != null) {
    // LOGGER.info("Le container existe");
    // } // else System.out.println("Le container n'existe pas.");

    if (obj.getApplication() == null || obj.getApplication().getAppId() == null) {
        LOGGER.error("you must specify the parent resource (parent Application) to manage containers.");
        return -1;
    }//from www  . j a va2  s.  com

    LOGGER.debug("Container to create in '" + obj.getApplication().getAppId() + "' : \n"
            + JAXBMapper.objectToXMLString(obj));
    LOGGER.info("Container to create in '" + obj.getApplication().getAppId() + "' : " + obj.getId());
    int resp = -1;
    try {
        resp = WebServiceActions.doPost(
                this.OM2MUrlBase + "applications/" + obj.getApplication().getAppId() + "/containers",
                JAXBMapper.objectToXMLString(obj), this.headers);
        LOGGER.info("Container '" + obj.getId() + "' creation in '" + obj.getId() + "' status : " + resp);
    } catch (HttpResponseException e) {
        resp = e.getStatusCode();
        LOGGER.error(e.getStatusCode() + " / " + e.getMessage() + ". " + this.OM2MUrlBase + "applications/"
                + obj.getApplication().getAppId() + "/containers");
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        LOGGER.error(e.getMessage());
    } catch (ConnectException e) {
        LOGGER.error(
                e.getMessage() + ". \nThe server '" + this.OM2MUrlBase.substring(7, 21) + "' is unreachable.");
    } catch (IOException e) {
        LOGGER.error(e.getMessage());
    }
    return resp;
}

From source file:com.puppetlabs.puppetdb.javaclient.impl.PuppetDBClientImpl.java

/**
 * Executes the request and converts the result into an object of the desired <code>type</code>. If the request results in a
 * {@link HttpStatus#SC_NOT_FOUND}, then this method will return <code>null</code>.
 *
 * @param uriStr//from  w  ww . j  ava  2 s  . c  om
 *            The relative path to the endpoint
 * @param params
 *            Parameters to pass in the request
 * @param type
 *            The expected return type
 * @return The response or <code>null</code> in case no data was found
 * @throws IOException
 */
protected <V> V getSingletonResponse(String uriStr, Map<String, String> params, Type type) throws IOException {
    try {
        return connector.get(uriStr, params, type);
    } catch (HttpResponseException e) {
        if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND)
            return null;
        throw e;
    }
}

From source file:fr.lissi.belilif.om2m.oao.ContentInstanceManager.java

@Override
public List<ContentInstance> getAll(Resource res) {
    Container container;/*  ww  w.  j  a va  2 s .  co  m*/
    if (res instanceof Container) {
        container = (Container) res;
    } else {
        LOGGER.error(
                "The parameter of the method 'ContainerManager->getAll' must be an instance of Application.");
        return null;
    }

    HttpGetSimpleResp resp = null;
    try {
        resp = WebServiceActions
                .doGet(this.OM2MUrlBase + "applications/" + container.getApplication().getAppId()
                        + "/containers/" + container.getId() + "/contentInstances", headers);
    } catch (HttpResponseException e) {
        LOGGER.error("HttpResponseException - " + e.getStatusCode() + " / " + e.getMessage());
    } catch (ClientProtocolException e) {
        LOGGER.error(e);
    } catch (ConnectException e) {
        LOGGER.error(
                e.getMessage() + ". \nThe server '" + this.OM2MUrlBase.substring(7, 21) + "' is unreachable.");
    } catch (IOException e) {
        LOGGER.error(e);
    }

    // xml to object
    ContentInstances contentInstancesResp = (ContentInstances) JAXBMapper.XMLStringToObject(resp.getResult(),
            ContentInstances.class);
    List<ContentInstance> contentInstances = new ArrayList<ContentInstance>();

    for (ContentInstance contentInstance : contentInstancesResp.getContentInstanceCollection()
            .getContentInstance()) {
        contentInstances.add(contentInstance);
    }

    return contentInstances;
}

From source file:com.puppetlabs.puppetdb.javaclient.impl.PuppetDBClientImpl.java

/**
 * Executes the request and converts the result into a map of the desired <code>type</code>. If the request results in a
 * {@link HttpStatus#SC_NOT_FOUND}, then this method will
 * return an empty map.//from   ww  w  .  j a  v  a  2s.co  m
 *
 * @param uriStr
 *            The relative path to the endpoint
 * @param params
 *            Parameters to pass in the request
 * @param type
 *            The expected return type (must be a generic List declaration)
 * @return The response in list form or an empty list in case no data was found
 * @throws IOException
 */
protected <K, V> Map<K, V> getMapResponse(String uriStr, Map<String, String> params, Type type)
        throws IOException {
    try {
        return connector.get(uriStr, params, type);
    } catch (HttpResponseException e) {
        if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND)
            return Collections.emptyMap();
        throw e;
    }
}

From source file:eu.fthevenet.binjr.data.adapters.HttpDataAdapterBase.java

protected <R> R doHttpGet(URI requestUri, ResponseHandler<R> responseHandler) throws DataAdapterException {
    try (Profiler p = Profiler.start("Executing HTTP request: [" + requestUri.toString() + "]",
            logger::trace)) {//from   w w w . j  a  va2  s .  c o  m
        logger.debug(() -> "requestUri = " + requestUri);
        HttpGet httpget = new HttpGet(requestUri);
        // Set user-agent pattern to workaround CAS server not proposing SPNEGO authentication unless it thinks agent can handle it.
        httpget.setHeader("User-Agent", "binjr/" + AppEnvironment.getInstance().getVersion()
                + " (Authenticates like: Firefox/Safari/Internet Explorer)");
        R result = httpClient.execute(httpget, responseHandler);
        if (result == null) {
            throw new FetchingDataFromAdapterException(
                    "Response entity to \"" + requestUri.toString() + "\" is null.");
        }
        return result;
    } catch (HttpResponseException e) {
        String msg;
        switch (e.getStatusCode()) {
        case 401:
            msg = "Authentication failed while trying to access \"" + requestUri.toString() + "\"";
            break;
        case 403:
            msg = "Access to the resource at \"" + requestUri.toString() + "\" is denied.";
            break;
        case 404:
            msg = "The resource at \"" + requestUri.toString() + "\" could not be found.";
            break;
        case 500:
            msg = "A server-side error has occurred while trying to access the resource at \""
                    + requestUri.toString() + "\": " + e.getMessage();
            break;
        default:
            msg = "Error executing HTTP request \"" + requestUri.toString() + "\": " + e.getMessage();
            break;
        }
        throw new SourceCommunicationException(msg, e);
    } catch (ConnectException e) {
        throw new SourceCommunicationException(e.getMessage(), e);
    } catch (UnknownHostException e) {
        throw new SourceCommunicationException("Host \"" + baseAddress.getHost()
                + (baseAddress.getPort() > 0 ? ":" + baseAddress.getPort() : "") + "\" could not be found.", e);
    } catch (IOException e) {
        throw new SourceCommunicationException("IO error while communicating with host \""
                + baseAddress.getHost() + (baseAddress.getPort() > 0 ? ":" + baseAddress.getPort() : "") + "\"",
                e);
    } catch (Exception e) {
        throw new SourceCommunicationException("Unexpected error in HTTP GET", e);
    }
}

From source file:au.csiro.casda.sodalint.ValidateServiceDescriptor.java

private String getSyncContent(final Reporter reporter, URL address) {
    try {/*from   w w w . ja v  a  2s.  c  om*/
        String content = getXmlContentFromUrl(address.toString());
        if (content == null) {
            reporter.report(SodaCode.E_SYCO, "Sync response contains no content");
        }
        return content;
    } catch (HttpResponseException e) {
        reporter.report(SodaCode.E_SYCO,
                "Unexpected http response: " + e.getStatusCode() + " Reason: " + e.getMessage());
    } catch (UnsupportedEncodingException e) {
        reporter.report(SodaCode.E_SYCO, "Sync response has an unexpected content type:" + e.getMessage());
    } catch (IOException e) {
        reporter.report(SodaCode.E_SYCO, "Unable to read sync response: " + e.getMessage());
    }

    return null;
}

From source file:com.puppetlabs.puppetdb.javaclient.impl.PuppetDBClientImpl.java

/**
 * Executes the request and converts the result into a list of the desired <code>type</code>. If the request results in a
 * {@link HttpStatus#SC_NOT_FOUND}, then this method will
 * return an empty list./*from w ww  .j  a v a  2  s .co m*/
 *
 * @param uriStr
 *            The relative path to the endpoint
 * @param params
 *            Parameters to pass in the request
 * @param type
 *            The expected return type (must be a generic List declaration)
 * @param paging
 *            The paging info or <code>null</code>.
 * @return The response in list form or an empty list in case no data was found
 * @throws IOException
 */
protected <V, Q> List<V> getListResponse(String uriStr, Parameters<Q> params, Type type) throws IOException {
    try {
        List<V> result;
        if (params instanceof Paging && ((Paging<?>) params).isIncludeTotal())
            result = connector.get(uriStr, (Paging<Q>) params, type);
        else
            result = connector.get(uriStr, paramsAsMap(params), type);
        return result;
    } catch (HttpResponseException e) {
        if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND)
            return Collections.emptyList();
        throw e;
    }
}