Example usage for org.apache.http.message BasicHeader BasicHeader

List of usage examples for org.apache.http.message BasicHeader BasicHeader

Introduction

In this page you can find the example usage for org.apache.http.message BasicHeader BasicHeader.

Prototype

public BasicHeader(String str, String str2) 

Source Link

Usage

From source file:com.hoccer.http.MultipartHttpEntity.java

@Override
public Header getContentType() {
    return new BasicHeader("Content-Type", "multipart/form-data; boundary=" + BORDER);
}

From source file:org.sharetask.controller.UserControllerIT.java

@Test
public void testReadUser() throws IOException {
    //given//  ww w. j a  v  a  2 s .  c  om
    final HttpGet httpGet = new HttpGet(URL_USER + "?username=dev1%40shareta.sk");
    httpGet.addHeader(new BasicHeader("Content-Type", "application/json"));

    //when
    final HttpResponse response = getClient().execute(httpGet);

    //then
    Assert.assertEquals(HttpStatus.OK.value(), response.getStatusLine().getStatusCode());
    final String responseData = EntityUtils.toString(response.getEntity());
    Assert.assertTrue(responseData.contains("\"name\":\"John\""));
    Assert.assertTrue(responseData.contains("\"surName\":\"Developer\""));
}

From source file:com.android.volley.stack.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request) throws IOException, AuthFailureError {
    HashMap<String, String> map = new HashMap<String, String>();
    if (!TextUtils.isEmpty(mUserAgent)) {
        map.put(HTTP.USER_AGENT, mUserAgent);
    }//  w  w  w .ja va 2s .c o  m
    map.putAll(request.getHeaders());

    URL parsedUrl = new URL(request.getUrl());
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }

    setConnectionParametersForRequest(connection, request);

    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }

    StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
            connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));

    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }

    return response;
}

From source file:leap.webunit.client.THttpRequestImpl.java

@Override
public THttpRequest setHeader(String name, String value) {
    Args.notNull(name, "Header name");
    headers.updateHeader(new BasicHeader(name, value));
    return this;
}

From source file:com.telefonica.iot.cygnus.backends.hdfs.HDFSBackendImplREST.java

/**
 * //from   w  ww  .j a  v a 2 s.  co m
 * @param hdfsHost
 * @param hdfsPort
 * @param hdfsUser
 * @param hdfsPassword
 * @param oauth2Token
 * @param hiveServerVersion
 * @param hiveHost
 * @param hivePort
 * @param krb5
 * @param krb5User
 * @param krb5Password
 * @param krb5LoginConfFile
 * @param krb5ConfFile
 * @param serviceAsNamespace
 * @param maxConns
 * @param maxConnsPerRoute
 */
public HDFSBackendImplREST(String hdfsHost, String hdfsPort, String hdfsUser, String hdfsPassword,
        String oauth2Token, String hiveServerVersion, String hiveHost, String hivePort, boolean krb5,
        String krb5User, String krb5Password, String krb5LoginConfFile, String krb5ConfFile,
        boolean serviceAsNamespace, int maxConns, int maxConnsPerRoute) {
    super(hdfsHost, hdfsPort, false, krb5, krb5User, krb5Password, krb5LoginConfFile, krb5ConfFile, maxConns,
            maxConnsPerRoute);
    this.hdfsHost = hdfsHost;
    this.hdfsPort = hdfsPort;
    this.hdfsUser = hdfsUser;
    this.serviceAsNamespace = serviceAsNamespace;

    // add the OAuth2 token as a the unique header that will be sent
    if (oauth2Token != null && oauth2Token.length() > 0) {
        headers = new ArrayList<>();
        headers.add(new BasicHeader("X-Auth-Token", oauth2Token));
        headers.add(new BasicHeader("Content-Type", "text/plain; charset=utf-8"));
    } else {
        headers = null;
    } // if else
}

From source file:eu.prestoprime.p4gui.connection.P4HttpClient.java

public HttpResponse executeRequest(HttpRequestBase request) throws IOException {
    // set userID
    request.setHeader(new BasicHeader("userID", this.userID));

    // disable redirect handling
    HttpParams params = new BasicHttpParams();
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    request.setParams(params);/*from   w ww  .j  a va 2 s .co m*/

    // execute request
    HttpResponse response = super.execute(request);

    // check redirect
    if (redirectCodes.contains(response.getStatusLine().getStatusCode())) {
        logger.debug("Redirecting...");

        // get newURL
        String newURL = response.getFirstHeader("Location").getValue();

        // create newRequest
        try {
            HttpUriRequest newRequest = request.getClass().getDeclaredConstructor(String.class)
                    .newInstance(newURL);

            // copy entity
            if (request instanceof HttpEntityEnclosingRequestBase) {
                HttpEntity entity = ((HttpEntityEnclosingRequestBase) request).getEntity();
                if (entity != null) {
                    logger.debug("Cloning entity...");

                    ((HttpEntityEnclosingRequestBase) newRequest).setEntity(entity);
                }
            }

            // set userID
            newRequest.setHeader(new BasicHeader("userID", this.userID));

            // retry
            response = new P4HttpClient(userID).execute(newRequest);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException
                | InstantiationException e) {
            e.printStackTrace();
        }
    }

    return response;
}

From source file:org.sharetask.controller.TaskControllerIT.java

@Test
public void testGetAllMyTask() throws IOException {
    //given/*from ww  w. j a v a2  s . co  m*/
    final HttpGet httpGet = new HttpGet(BASE_URL + "/workspace/tasks" + "/?taskQueue=ALL_MY");
    httpGet.addHeader(new BasicHeader("Content-Type", "application/json"));

    //when
    final HttpResponse response = getClient().execute(httpGet);

    //then
    Assert.assertEquals(HttpStatus.OK.value(), response.getStatusLine().getStatusCode());
    final String responseData = EntityUtils.toString(response.getEntity());
    Assert.assertTrue(responseData.contains("\"username\":\"dev1@shareta.sk\""));
}

From source file:com.arcbees.vcs.AbstractVcsApi.java

protected void setDefaultHeaders(HttpUriRequest request) {
    request.setHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, CharEncoding.UTF_8));
    request.setHeader(new BasicHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()));
}

From source file:com.everis.storage.service.StorageService.java

private void restClient(Transaction transaction) {
    HttpClient httpClient = HttpClientBuilder.create().build();
    try {/*from  www  .  j av a2  s.  c o m*/
        InstanceInfo service = discoverService();
        System.out.println("DISCOVERY: " + service);
        // specify the host, protocol, and port            
        HttpHost target = new HttpHost(service.getHostName(), service.getPort(), "http");

        // specify the get request
        HttpPost postRequest = new HttpPost("/transactions");

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").create();
        StringEntity jsonEntity = new StringEntity(gson.toJson(transaction));

        postRequest.setEntity(jsonEntity);
        postRequest.addHeader(new BasicHeader("Content-type", "application/json"));

        System.out.println("executing request to " + target);

        HttpResponse httpResponse = httpClient.execute(target, postRequest);
        HttpEntity entity = httpResponse.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(httpResponse.getStatusLine());
        Header[] headers = httpResponse.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            System.out.println(headers[i]);
        }
        System.out.println("----------------------------------------");

        if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpClient.getConnectionManager().shutdown();
    }
}