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

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

Introduction

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

Prototype

void setHeader(String str, String str2);

Source Link

Usage

From source file:org.openestate.is24.restapi.hc42.HttpComponents42Client.java

@Override
protected Response sendXmlAttachmentRequest(URL url, RequestMethod method, String xml, InputStream input,
        String fileName, String mimeType) throws IOException, OAuthException {
    if (method == null)
        method = RequestMethod.POST;/*from  ww  w  . ja  v a 2  s  .c om*/
    if (!RequestMethod.POST.equals(method) && !RequestMethod.PUT.equals(method))
        throw new IllegalArgumentException("Invalid request method!");
    xml = (RequestMethod.POST.equals(method) || RequestMethod.PUT.equals(method)) ? StringUtils.trimToNull(xml)
            : null;

    HttpUriRequest request = null;
    if (RequestMethod.POST.equals(method)) {
        request = new HttpPost(url.toString());
    } else if (RequestMethod.PUT.equals(method)) {
        request = new HttpPut(url.toString());
    } else {
        throw new IOException("Unsupported request method '" + method + "'!");
    }

    MultipartEntity requestMultipartEntity = new MultipartEntity();
    request.addHeader(requestMultipartEntity.getContentType());
    request.setHeader("Content-Language", "en-US");
    request.setHeader("Accept", "application/xml");

    // add xml part to the multipart entity
    if (xml != null) {
        //StringBody xmlPart = new StringBody(
        //  xml, "application/xml; name=body.xml", Charset.forName( getEncoding() ) );
        InputStreamBody xmlPart = new InputStreamBody(new ByteArrayInputStream(xml.getBytes(getEncoding())),
                "application/xml", "body.xml");
        requestMultipartEntity.addPart("metadata", xmlPart);
    }

    // add file part to the multipart entity
    if (input != null) {
        mimeType = StringUtils.trimToNull(mimeType);
        if (mimeType == null)
            mimeType = "application/octet-stream";

        fileName = StringUtils.trimToNull(fileName);
        if (fileName == null)
            fileName = "upload.bin";

        InputStreamBody filePart = new InputStreamBody(input, mimeType, fileName);
        requestMultipartEntity.addPart("attachment", filePart);
    }

    // add multipart entity to the request
    ((HttpEntityEnclosingRequest) request).setEntity(requestMultipartEntity);

    // sign request
    getAuthConsumer().sign(request);

    // send request
    HttpResponse response = httpClient.execute(request);

    // create response
    return createResponse(response);
}

From source file:io.personium.client.http.RestAdapter.java

/**
 * This is the PUT method that receives the response body.
 * @param url Target Request URL//from   w  ww .  j a va2s .  c o  m
 * @param map Hash map of Request Header
 * @return DcResponse object
 * @throws DaoException Exception thrown
 */
public PersoniumResponse put(String url, HashMap<String, String> map) throws DaoException {
    HttpUriRequest req = new PersoniumRequestBuilder().url(url).method(HttpMethods.PUT).token(getToken())
            .defaultHeaders(this.accessor.getDefaultHeaders()).build();
    for (Map.Entry<String, String> entry : map.entrySet()) {
        req.setHeader((String) entry.getKey(), (String) entry.getValue());
    }
    return this.request(req);
}

From source file:com.nononsenseapps.notepad.sync.googleapi.GoogleAPITalker.java

/**
 * Sets the authorization header//from w w w. ja  va 2 s.  c  o  m
 * 
 * @param request
 * @return
 */
private void setAuthHeader(HttpUriRequest request) {
    if (request != null)
        request.setHeader("Authorization", "OAuth " + authToken);
}

From source file:com.fujitsu.dc.client.http.RestAdapter.java

/**
 * This is the POST method that receives the request body that does not need authorization and uses header map.
 * @param url Target URL//from   ww  w . j  a v  a2  s .co m
 * @param map HashMap of Request Header
 * @param data Data to be written
 * @param contentType CONTENT-TYPE value
 * @return DcResponse object
 * @throws DaoException Exception thrown
 */
public DcResponse post(String url, HashMap<String, String> map, String data, String contentType)
        throws DaoException {
    HttpUriRequest req = new DcRequestBuilder().url(url).method(HttpMethods.POST).contentType(contentType)
            .body(data).token(getToken()).defaultHeaders(this.accessor.getDefaultHeaders()).build();
    for (Map.Entry<String, String> entry : map.entrySet()) {
        req.setHeader(entry.getKey(), entry.getValue());
    }
    return this.request(req);
}

From source file:com.fujitsu.dc.client.http.RestAdapter.java

/**
 * This is the GET method to receive the response body using headers (If-None-Match specified).
 * @param url Target Request URL/*ww  w .  j a v a 2 s .  c  o m*/
 * @param headers Request header
 * @param etag Etag value
 * @return DcResponse object
 * @throws DaoException Exception thrown
 */
public DcResponse get(String url, Map<String, String> headers, String etag) throws DaoException {
    HttpUriRequest req = new DcRequestBuilder().url(url).method(HttpMethods.GET).acceptEncoding("gzip")
            .token(getToken()).ifNoneMatch(etag).defaultHeaders(this.accessor.getDefaultHeaders()).build();
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        req.setHeader(entry.getKey(), entry.getValue());
    }
    return this.request(req);
}

From source file:com.nononsenseapps.notepad.sync.googleapi.GoogleAPITalker.java

/**
 * Does nothing if etag is null or "" Sets an if-none-match header for weak
 * etag comparisons./*from w w  w  .j  a v  a 2 s .c  om*/
 * 
 * If-None-Match: W/"D08FQn8-eil7ImA9WxZbFEw."
 * 
 * @param etag
 */
private void setHeaderWeakEtag(HttpUriRequest httpget, String etag) {
    if (etag != null && !etag.equals("")) {
        httpget.setHeader("If-None-Match", etag);

        //Log.d(TAG, "If-None-Match: " + etag);
    }
}

From source file:io.personium.client.http.RestAdapter.java

/**
 * This is the GET method to receive the response body using headers (If-None-Match specified).
 * @param url Target Request URL/*  w  w w.ja v  a 2 s  .  co  m*/
 * @param headers Request header
 * @param etag Etag value
 * @return DcResponse object
 * @throws DaoException Exception thrown
 */
public PersoniumResponse get(String url, Map<String, String> headers, String etag) throws DaoException {
    HttpUriRequest req = new PersoniumRequestBuilder().url(url).method(HttpMethods.GET).acceptEncoding("gzip")
            .token(getToken()).ifNoneMatch(etag).defaultHeaders(this.accessor.getDefaultHeaders()).build();
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        req.setHeader(entry.getKey(), entry.getValue());
    }
    return this.request(req);
}

From source file:com.nononsenseapps.notepad.sync.googleapi.GoogleAPITalker.java

/**
 * Does nothing if etag is null or "" Sets an if-match header for strong
 * etag comparisons.//from  w w w . j a  va2  s.c  o m
 * 
 * @param etag
 */
private void setHeaderStrongEtag(final HttpUriRequest httppost, final String etag) {
    if (etag != null && !etag.isEmpty()) {
        httppost.setHeader("If-Match", etag);

        //Log.d(TAG, "If-Match: " + etag);
    } else {
        //Log.d(TAG, "No ETAG could be found!");
    }
}

From source file:com.fujitsu.dc.client.http.RestAdapter.java

/**
 * This is the PUT method that receives the response body and uses Etag value and header map.
 * @param url Target Request URL/* w  w w  . j av  a2s.c  o  m*/
 * @param data Data to be sent
 * @param etag ETag value
 * @param map HashMap of Request Header
 * @param contentType CONTENT-TYPE value
 * @return DcResponse object
 * @throws DaoException Exception thrown
 */
public DcResponse put(String url, String data, String etag, HashMap<String, String> map, String contentType)
        throws DaoException {
    HttpUriRequest req = new DcRequestBuilder().url(url).method(HttpMethods.PUT).contentType(contentType)
            .ifMatch(etag).body(data).token(getToken()).defaultHeaders(this.accessor.getDefaultHeaders())
            .build();
    for (Map.Entry<String, String> entry : map.entrySet()) {
        req.setHeader((String) entry.getKey(), (String) entry.getValue());
    }
    return this.request(req);
}

From source file:io.personium.client.http.RestAdapter.java

/**
 * This is the POST method that receives the request body that does not need authorization and uses header map.
 * @param url Target URL/*from  w  w  w  .jav a  2s. c  o m*/
 * @param map HashMap of Request Header
 * @param data Data to be written
 * @param contentType CONTENT-TYPE value
 * @return DcResponse object
 * @throws DaoException Exception thrown
 */
public PersoniumResponse post(String url, HashMap<String, String> map, String data, String contentType)
        throws DaoException {
    HttpUriRequest req = new PersoniumRequestBuilder().url(url).method(HttpMethods.POST)
            .contentType(contentType).body(data).token(getToken())
            .defaultHeaders(this.accessor.getDefaultHeaders()).build();
    for (Map.Entry<String, String> entry : map.entrySet()) {
        req.setHeader(entry.getKey(), entry.getValue());
    }
    return this.request(req);
}