Example usage for org.apache.http.impl.client SystemDefaultHttpClient SystemDefaultHttpClient

List of usage examples for org.apache.http.impl.client SystemDefaultHttpClient SystemDefaultHttpClient

Introduction

In this page you can find the example usage for org.apache.http.impl.client SystemDefaultHttpClient SystemDefaultHttpClient.

Prototype

public SystemDefaultHttpClient(final HttpParams params) 

Source Link

Usage

From source file:org.geomajas.layer.common.proxy.LayerHttpServiceImpl.java

public InputStream getStream(final String baseUrl, final LayerAuthentication authentication,
        final String layerId) throws IOException {
    // Create a HTTP client object, which will initiate the connection:
    final HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT);
    SystemDefaultHttpClient client = new SystemDefaultHttpClient(httpParams);

    String url = addCredentialsToUrl(baseUrl, authentication);

    // -- add basic authentication
    if (null != authentication
            && (LayerAuthenticationMethod.BASIC.equals(authentication.getAuthenticationMethod())
                    || LayerAuthenticationMethod.DIGEST.equals(authentication.getAuthenticationMethod()))) {
        // Set up the credentials:
        Credentials creds = new UsernamePasswordCredentials(authentication.getUser(),
                authentication.getPassword());
        AuthScope scope = new AuthScope(parseDomain(url), parsePort(url), authentication.getRealm());
        client.getCredentialsProvider().setCredentials(scope, creds);
    }//from  w w  w  . j  a  va  2  s  .c  om

    // -- add interceptors if any --
    addInterceptors(client, baseUrl, layerId);

    // Create the GET method with the correct URL:
    HttpGet get = new HttpGet(url);

    // Execute the GET:
    HttpResponse response = client.execute(get);
    log.debug("Response: {} - {}", response.getStatusLine().getStatusCode(),
            response.getStatusLine().getReasonPhrase());

    return new LayerHttpServiceStream(response, client);
}

From source file:org.apache.jena.fuseki.http.DatasetGraphAccessorHTTP.java

private Graph exec(String targetStr, Graph graphToSend, HttpUriRequest httpRequest, boolean processBody) {
    HttpClient httpclient = new SystemDefaultHttpClient(httpParams);

    if (graphToSend != null) {
        // ??? httpRequest isa Post
        // Impedence mismatch - is there a better way?
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Model model = ModelFactory.createModelForGraph(graphToSend);
        model.write(out, "RDF/XML");
        byte[] bytes = out.toByteArray();
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        InputStreamEntity reqEntity = new InputStreamEntity(in, bytes.length);
        reqEntity.setContentType(WebContent.contentTypeRDFXML);
        reqEntity.setContentEncoding(WebContent.charsetUTF8);
        HttpEntity entity = reqEntity;/* www.j a v a2s.c  o m*/
        ((HttpEntityEnclosingRequestBase) httpRequest).setEntity(entity);
    }
    TypedInputStream ts = null;
    // httpclient.getParams().setXXX
    try {
        HttpResponse response = httpclient.execute(httpRequest);

        int responseCode = response.getStatusLine().getStatusCode();
        String responseMessage = response.getStatusLine().getReasonPhrase();

        if (HttpSC.isRedirection(responseCode))
            // Not implemented yet.
            throw FusekiRequestException.create(responseCode, responseMessage);

        // Other 400 and 500 - errors

        if (HttpSC.isClientError(responseCode) || HttpSC.isServerError(responseCode))
            throw FusekiRequestException.create(responseCode, responseMessage);

        if (responseCode == HttpSC.NO_CONTENT_204)
            return null;
        if (responseCode == HttpSC.CREATED_201)
            return null;

        if (responseCode != HttpSC.OK_200) {
            Log.warn(this, "Unexpected status code");
            throw FusekiRequestException.create(responseCode, responseMessage);
        }

        // May not have a body.
        String ct = getHeader(response, HttpNames.hContentType);
        if (ct == null) {
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();
                // Read to completion?
                instream.close();
            }
            return null;
        }

        // Tidy. See ConNeg / MediaType.
        String x = getHeader(response, HttpNames.hContentType);
        String y[] = x.split(";");
        String contentType = null;
        if (y[0] != null)
            contentType = y[0].trim();
        String charset = null;
        if (y.length > 1 && y[1] != null)
            charset = y[1].trim();

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream instream = entity.getContent();
            //                String mimeType = ConNeg.chooseContentType(request, rdfOffer, ConNeg.acceptRDFXML).getAcceptType() ;
            //                String charset = ConNeg.chooseCharset(request, charsetOffer, ConNeg.charsetUTF8).getAcceptType() ;
            ts = new TypedInputStream(instream, contentType, charset, null);
        }
        Graph graph = GraphFactory.createGraphMem();
        if (processBody)
            readGraph(graph, ts, null);
        if (ts != null)
            ts.close();
        Graph graph2 = new UnmodifiableGraph(graph);
        return graph2;
    } catch (IOException ex) {
        httpRequest.abort();
        return null;
    }
}