Example usage for org.apache.http.client.methods HttpUriRequest getRequestLine

List of usage examples for org.apache.http.client.methods HttpUriRequest getRequestLine

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest getRequestLine.

Prototype

RequestLine getRequestLine();

Source Link

Usage

From source file:org.dalmasso.ietfsched.io.RemoteExecutor.java

/**
 * Execute a {@link HttpGet} request, passing a valid response through
 * {@link XmlHandler#parseAndApply(XmlPullParser, ContentResolver)}.
 *///from  w  w  w  .  j a  v a2  s. c o  m
public InputStream executeGet(String url) throws Exception {
    // Last-Modified Thu, 01 Sep 2011 16:11:33 GMT
    //ETag "aa4a6d-41d1-4abe37f915740"-gzip

    HttpUriRequest request = new HttpGet(url);
    HttpResponse resp = mHttpClient.execute(request);
    int status = resp.getStatusLine().getStatusCode();

    if (status != HttpStatus.SC_OK) {
        Log.d(TAG, "Response Code " + status);
        throw new IOException(
                "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine());
    }
    return resp.getEntity().getContent();
}

From source file:net.peterkuterna.android.apps.devoxxfrsched.io.RemoteExecutor.java

/**
 * Execute this {@link HttpUriRequest}, passing a valid response through
 * {@link JsonHandler#parseAndApply(JsonParser, ContentResolver)}.
 *//*from ww  w . j  a  v a 2 s  .  c o  m*/
public void execute(HttpUriRequest request, JsonHandler handler) throws HandlerException {
    try {
        final HttpResponse resp = mHttpClient.execute(request);
        final int status = resp.getStatusLine().getStatusCode();
        if (status != HttpStatus.SC_OK) {
            throw new HandlerException(
                    "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine());
        }

        final InputStream input = resp.getEntity().getContent();
        try {
            final JsonParser parser = JsonHandlerUtils.newJsonParser(input);
            handler.parseAndApply(parser, mResolver);
        } catch (JsonParseException e) {
            throw new HandlerException("Malformed response for " + request.getRequestLine(), e);
        } finally {
            if (input != null)
                input.close();
        }
    } catch (HandlerException e) {
        throw e;
    } catch (IOException e) {
        throw new HandlerException("Problem reading remote response for " + request.getRequestLine(), e);
    }
}

From source file:com.infine.android.devoxx.io.json.TwitterRemoteExecutor.java

private <T> T execute(HttpUriRequest request, Class<T> mappingClass)
        throws JsonProcessingException, IOException {
    T jsonObjects = null;/*from  w w w  . j  a v a  2  s .c om*/

    final HttpResponse resp = mHttpClient.execute(request);
    final int status = resp.getStatusLine().getStatusCode();
    if (status != HttpStatus.SC_OK) {
        throw new IOException(
                "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine());
    }
    final InputStream input = resp.getEntity().getContent();
    try {
        jsonObjects = readData(input, mappingClass);
    } finally {
        if (input != null)
            input.close();
    }
    return jsonObjects;

}

From source file:ca.mudar.mtlaucasou.io.RemoteExecutor.java

/**
 * Execute this {@link HttpUriRequest}, passing a valid response through
 * {@link XmlHandler#parseAndApply(XmlPullParser, ContentResolver)}.
 *///from  w w  w  . ja v  a 2 s  . c om
public void execute(HttpUriRequest request, XmlHandler handler) throws HandlerException {
    try {
        final HttpResponse resp = mHttpClient.execute(request);
        final int status = resp.getStatusLine().getStatusCode();
        if (status != HttpStatus.SC_OK) {
            throw new HandlerException(
                    "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine());
        }

        final InputStream input = resp.getEntity().getContent();
        try {
            final XmlPullParser parser = ParserUtils.newPullParser(input);
            handler.parseAndApply(parser, mResolver);
        } catch (XmlPullParserException e) {
            throw new HandlerException("Malformed response for " + request.getRequestLine(), e);
        } finally {
            if (input != null)
                input.close();
        }
    } catch (HandlerException e) {
        throw e;
    } catch (IOException e) {
        throw new HandlerException("Problem reading remote response for " + request.getRequestLine(), e);
    }
}

From source file:org.dalmasso.ietfsched.io.RemoteExecutor.java

public String executeHead(String url) throws Exception {
    final HttpUriRequest request = new HttpHead(url);
    final HttpResponse resp = mHttpClient.execute(request);
    final int status = resp.getStatusLine().getStatusCode();
    if (status != HttpStatus.SC_OK) {
        Log.d(TAG, "Response Code " + status);
        throw new IOException(
                "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine());
    }/*w ww .j a v a  2s  .  c o m*/
    //      Header h = resp.getFirstHeader("Content-Length");
    Header h = resp.getFirstHeader("Etag");
    if (h != null) {
        try {
            //            int length = Integer.parseInt(h.getValue());
            //            Log.d(TAG, "Content-Length " + length);
            String etag = h.getValue();
            Log.d(TAG, "Etag " + h.getValue());
            return etag;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    return null;
}

From source file:fr.openbike.android.io.RemoteExecutor.java

public Object execute(HttpUriRequest request, JSONHandler handler, Context context) throws HandlerException {
    try {/*from w ww . j  ava  2  s. c om*/
        final HttpResponse resp = mHttpClient.execute(request);
        final int status = resp.getStatusLine().getStatusCode();
        if (status != HttpStatus.SC_OK) {
            throw new HandlerException(
                    "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine());
        }

        final InputStream input = resp.getEntity().getContent();
        Object result = null;
        try {
            result = handler.parse(new JSONObject(ParserUtils.getString(input)),
                    OpenBikeDBAdapter.getInstance(context));
            if (input != null)
                input.close();
            return result;
        } catch (JSONException e) {
            if (input != null)
                input.close();
            throw new HandlerException("Malformed response for " + request.getRequestLine(), e);
        }
    } catch (HandlerException e) {
        throw e;
    } catch (IOException e) {
        throw new HandlerException("Problem reading remote response for " + request.getRequestLine(), e);
    }
}

From source file:io.ucoin.ucoinj.core.client.service.HttpServiceImpl.java

@SuppressWarnings("unchecked")
protected <T> T executeRequest(HttpClient httpClient, HttpUriRequest request, Class<? extends T> resultClass) {
    T result = null;//from   w w w. j a  v  a 2s .c o  m

    if (log.isDebugEnabled()) {
        log.debug("Executing request : " + request.getRequestLine());
    }

    HttpResponse response = null;
    try {
        response = httpClient.execute(request);

        if (log.isDebugEnabled()) {
            log.debug("Received response : " + response.getStatusLine());
        }

        switch (response.getStatusLine().getStatusCode()) {
        case HttpStatus.SC_OK: {
            result = (T) parseResponse(response, resultClass);

            response.getEntity().consumeContent();
            break;
        }
        case HttpStatus.SC_UNAUTHORIZED:
        case HttpStatus.SC_FORBIDDEN:
            throw new TechnicalException("ucoinj.client.authentication");
        case HttpStatus.SC_BAD_REQUEST:
            throw new HttpBadRequestException("ucoinj.client.status" + response.getStatusLine().toString());
        default:
            throw new TechnicalException("ucoinj.client.status" + response.getStatusLine().toString());
        }
    } catch (ConnectException e) {
        throw new TechnicalException("ucoinj.client.core.connect", e);
    } catch (SocketTimeoutException e) {
        throw new TechnicalException("ucoinj.client.core.timeout", e);
    } catch (IOException e) {
        throw new TechnicalException(e.getMessage(), e);
    } finally {
        // Close is need
        if (response instanceof CloseableHttpResponse) {
            try {
                ((CloseableHttpResponse) response).close();
            } catch (IOException e) {
                // Silent is gold
            }
        }
    }

    return result;
}

From source file:com.socialize.net.AsyncHttpRequestProcessor.java

@Override
protected AsyncHttpResponse doInBackground(AsyncHttpRequest... params) {

    AsyncHttpRequest request = params[0];
    AsyncHttpResponse response = new AsyncHttpResponse();
    response.setRequest(request);//  w  w w .  j  a  va2  s  .c  o m

    try {
        HttpUriRequest httpRequest = request.getRequest();

        if (!clientFactory.isDestroyed()) {
            if (logger != null && logger.isDebugEnabled()) {
                logger.debug(
                        "Request: " + httpRequest.getMethod() + " " + httpRequest.getRequestLine().getUri());

                StringBuilder builder = new StringBuilder();
                Header[] allHeaders = httpRequest.getAllHeaders();

                for (Header header : allHeaders) {
                    builder.append(header.getName());
                    builder.append(":");
                    builder.append(header.getValue());
                    builder.append("\n");
                }

                logger.debug("REQUEST \nurl:[" + httpRequest.getURI().toString() + "] \nheaders:\n"
                        + builder.toString());

                if (httpRequest instanceof HttpPost) {
                    HttpPost post = (HttpPost) httpRequest;
                    HttpEntity entity = post.getEntity();

                    if (!(entity instanceof MultipartEntity)) {
                        String requestData = ioUtils.readSafe(entity.getContent());
                        logger.debug("REQUEST \ndata:[" + requestData + "]");
                    }
                }
            }

            HttpClient client = clientFactory.getClient();

            HttpResponse httpResponse = client.execute(httpRequest);

            response.setResponse(httpResponse);

            if (logger != null && logger.isDebugEnabled()) {
                logger.debug("RESPONSE CODE: " + httpResponse.getStatusLine().getStatusCode());
            }

            HttpEntity entity = null;

            try {
                entity = httpResponse.getEntity();

                if (httpUtils.isHttpError(httpResponse)) {
                    String msg = ioUtils.readSafe(entity.getContent());
                    throw new SocializeApiError(httpUtils, httpResponse.getStatusLine().getStatusCode(), msg);
                } else {
                    String responseData = ioUtils.readSafe(entity.getContent());

                    if (logger != null && logger.isDebugEnabled()) {
                        logger.debug("RESPONSE: " + responseData);
                    }

                    response.setResponseData(responseData);
                }
            } finally {
                closeEntity(entity);
            }
        } else {
            throw new SocializeException("Cannot execute http request.  HttpClient factory was destroyed.");
        }
    } catch (Exception e) {
        response.setError(e);
    }

    return response;

}

From source file:scouter.xtra.httpclient.HttpClient43.java

public String getURI(Object o) {
    if (o instanceof HttpUriRequest) {
        HttpUriRequest req = (HttpUriRequest) o;
        return req.getURI().getPath();
    } else if (o instanceof HttpGet) {
        HttpGet req = (HttpGet) o;/* w ww .  j  av  a 2s . c om*/
        return req.getURI().getPath();
    } else if (o instanceof HttpPut) {
        HttpPut req = (HttpPut) o;
        return req.getURI().getPath();
    } else if (o instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) o;
        return req.getRequestLine().getUri();
    }
    return o.toString();
}

From source file:ca.mudar.parkcatcher.io.RemoteExecutor.java

/**
 * Execute this {@link HttpUriRequest}, passing a valid response through
 * {@link XmlHandler#parseAndApply(XmlPullParser, ContentResolver)}.
 *//*from  ww  w  .  j a v a 2  s.  co m*/
public void execute(HttpUriRequest request, JsonHandler handler) throws HandlerException {
    try {
        final HttpResponse resp = mHttpClient.execute(request);
        final int status = resp.getStatusLine().getStatusCode();
        if (status != HttpStatus.SC_OK) {
            throw new HandlerException(
                    "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine());
        }

        final InputStream input = resp.getEntity().getContent();
        try {
            final JSONTokener parser = ParserUtils.newJsonTokenerParser(input);
            handler.parseAndApply(parser, mResolver);
        } finally {
            if (input != null)
                input.close();
        }
    } catch (HandlerException e) {
        throw e;
    } catch (IOException e) {
        throw new HandlerException("Problem reading remote response for " + request.getRequestLine(), e);
    }
}