Example usage for org.apache.commons.httpclient Header Header

List of usage examples for org.apache.commons.httpclient Header Header

Introduction

In this page you can find the example usage for org.apache.commons.httpclient Header Header.

Prototype

public Header() 

Source Link

Usage

From source file:com.mycompany.neo4jprotein.neoStart.java

static public void addProperty(String nodeURI, String propertyName, String propertyValue) {
    String output = null;//from w ww  .ja va  2 s  . c o  m

    try {
        String nodePointUrl = nodeURI + "/properties/" + propertyName;
        HttpClient client = new HttpClient();
        PutMethod mPut = new PutMethod(nodePointUrl);

        /**
         * set headers
         */
        Header mtHeader = new Header();
        mtHeader.setName("content-type");
        mtHeader.setValue("application/json");
        mtHeader.setName("accept");
        mtHeader.setValue("application/json");
        mPut.addRequestHeader(mtHeader);

        /**
         * set json payload
         */
        String jsonString = "" + propertyValue + "";
        StringRequestEntity requestEntity = new StringRequestEntity(jsonString, "application/json", "UTF-8");
        mPut.setRequestEntity(requestEntity);
        int satus = client.executeMethod(mPut);
        output = mPut.getResponseBodyAsString();

        mPut.releaseConnection();
        System.out.println("satus : " + satus);
        System.out.println("output : " + output);
    } catch (Exception e) {
        System.out.println("Exception in creating node in neo4j : " + e);
    }

}

From source file:com.mycompany.neo4jprotein.neoStart.java

static public String createNode() {
    String output = null;/*from  www.  j  av a  2 s  .com*/
    String location = null;
    try {
        String nodePointUrl = SERVER_ROOT_URI + "/db/data/node/1";
        HttpClient client = new HttpClient();
        PostMethod mPost = new PostMethod(nodePointUrl);

        /**
         * set headers
         */
        Header mtHeader = new Header();
        mtHeader.setName("content-type");
        mtHeader.setValue("application/json");
        mtHeader.setName("accept");
        mtHeader.setValue("application/json");
        mPost.addRequestHeader(mtHeader);

        /**
         * set json payload
         */
        StringRequestEntity requestEntity = new StringRequestEntity("{}", "application/json", "UTF-8");
        mPost.setRequestEntity(requestEntity);
        int satus = client.executeMethod(mPost);
        output = mPost.getResponseBodyAsString();
        Header locationHeader = mPost.getResponseHeader("location");
        location = locationHeader.getValue();
        mPost.releaseConnection();
        System.out.println("satus : " + satus);
        System.out.println("location : " + location);
        System.out.println("output : " + output);
    } catch (Exception e) {
        System.out.println("Exception in creating node in neo4j : " + e);
    }

    return location;
}

From source file:net.duckling.ddl.service.resource.impl.ResourcePipeAgentServiceImpl.java

private JSONObject writeJson(JSONObject json) throws IOException {
    PostMethod method = new PostMethod(getQueryDomain() + "/pipe");
    method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
    Header h = new Header();
    h.setName("content-type");
    h.setValue("application/json");
    method.addRequestHeader(h);/* ww w. j  ava2s  .  c o m*/
    StringRequestEntity body = new StringRequestEntity(json.toString());
    method.setRequestEntity(body);
    try {
        int status = getHttpClient().executeMethod(method);
        if (status >= 200 && status < 300) {
            String result = getResponseBody(method.getResponseBodyAsStream());
            return new JSONObject(result);
        } else {
            return null;
        }
    } catch (HttpException e) {
        new IOException(e);
    } catch (IOException e) {
        throw e;
    } catch (ParseException e) {
        new IOException(e);
    } finally {
        method.releaseConnection();
    }
    return null;
}

From source file:net.duckling.ddl.service.resource.impl.ResourcePipeAgentServiceImpl.java

private String queryStatus(String taskId) throws IOException {
    GetMethod method = new GetMethod();
    method.setPath(getQueryDomain() + "/query?pipeTaskId=" + taskId);
    method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
    Header h = new Header();
    h.setName("content-type");
    h.setValue("application/json");
    method.addRequestHeader(h);//from w w  w.  j  a  v  a 2s.c o m
    try {
        int status = getHttpClient().executeMethod(method);
        if (status >= 200 && status < 300) {
            return getResponseBody(method.getResponseBodyAsStream());
        } else {
            return "";
        }
    } catch (HttpException e) {
        new IOException(e);
    } catch (IOException e) {
        throw e;
    } finally {
        method.releaseConnection();
    }
    return "";
}

From source file:com.intuit.tank.http.BaseRequestHandlerTest.java

/**
 * Run the void processResponse(byte[],long,long,BaseResponse,String,int,Header[],HttpState) method test.
 *
 * @throws Exception//from   www  .  j a v  a  2 s .  co  m
 *
 * @generatedBy CodePro at 12/16/14 3:57 PM
 */
@Test
public void testProcessResponse_1() throws Exception {
    byte[] bResponse = new byte[] {};
    long startTime = 1L;
    long endTime = 1L;
    BaseResponse response = new BinaryResponse();
    String message = "";
    int httpCode = 1;
    Header[] headers = new Header[] { new Header() };
    HttpState httpstate = new HttpState();

    BaseRequestHandler.processResponse(bResponse, startTime, endTime, response, message, httpCode, headers,
            httpstate);

    // An unexpected exception was thrown in user code while executing this test:
    //    java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.LogManager
    //       at org.apache.log4j.Logger.getLogger(Logger.java:117)
    //       at com.intuit.tank.http.BaseResponse.<clinit>(BaseResponse.java:18)
}

From source file:com.intuit.tank.http.BaseRequestHandlerTest.java

/**
 * Run the void processResponse(byte[],long,long,BaseResponse,String,int,Header[],HttpState) method test.
 *
 * @throws Exception/*from   ww  w  . j  a v  a2  s. c om*/
 *
 * @generatedBy CodePro at 12/16/14 3:57 PM
 */
@Test
public void testProcessResponse_6() throws Exception {
    byte[] bResponse = new byte[] {};
    long startTime = 1L;
    long endTime = 1L;
    BaseResponse response = new BinaryResponse();
    String message = "";
    int httpCode = 1;
    Header[] headers = new Header[] { new Header() };
    HttpState httpstate = new HttpState();

    BaseRequestHandler.processResponse(bResponse, startTime, endTime, response, message, httpCode, headers,
            httpstate);

    // An unexpected exception was thrown in user code while executing this test:
    //    java.lang.NoClassDefFoundError: Could not initialize class com.intuit.tank.http.binary.BinaryResponse
}

From source file:edu.harvard.iq.dvn.core.web.servlet.FileDownloadServlet.java

private void setDownloadContentHeaders(FileDownloadObject fileDownloadObject) {
    List<Header> headerList = new ArrayList();
    Header contentHeader = null;//from   www .ja  v  a 2s . co  m
    int headerCounter = 0;

    if (fileDownloadObject.getFileName() != null) {
        if (fileDownloadObject.getMimeType() != null) {

            // The "content-disposition" header is for the
            // Mozilla family of browsers;

            // about the commented out code below:
            //
            // the idea used to be that we should prompt
            // for the content to open in the browser
            // whenever possible; but lately it's been
            // suggested by our users that all downloads
            // should behave the same, i.e. prompt the
            // user to "save as" the file.

            //if ( dbContentType.equalsIgnoreCase("application/pdf") ||
            //     dbContentType.equalsIgnoreCase("text/xml") ||
            //     dbContentType.equalsIgnoreCase("text/plain"))  {
            //    res.setHeader ( "Content-disposition",
            //          "inline; filename=\"" + dbFileName + "\"" );
            //
            //} else {

            //respHeaders = new Header[2];
            contentHeader = new Header();

            contentHeader.setName("Content-disposition");
            contentHeader.setValue("attachment; filename=\"" + fileDownloadObject.getFileName() + "\"");

            headerList.add(contentHeader);
            headerCounter++;

            //}

            // And this one is for MS Explorer:

            contentHeader = new Header();

            contentHeader.setName("Content-Type");
            contentHeader.setValue(fileDownloadObject.getMimeType() + "; name=\""
                    + fileDownloadObject.getFileName() + "\"; charset=ISO-8859-1");

            headerList.add(contentHeader);
            headerCounter++;

        } else {
            // Have filename, but no content-type;
            // All we can do is provide a Mozilla-friendly
            // header:

            //respHeaders = new Header[1];

            contentHeader = new Header();

            contentHeader.setName("Content-disposition");
            contentHeader.setValue("attachment; filename=\"" + fileDownloadObject.getFileName() + "\"");

            headerList.add(contentHeader);
            headerCounter++;
        }
    } else if (fileDownloadObject.getMimeType() != null) {
        // no filename available;
        // but if we have content-type in the database
        // we'll just set that:

        //respHeaders = new Header[1];

        contentHeader = new Header();

        contentHeader.setName("Content-Type");
        contentHeader.setValue(fileDownloadObject.getMimeType());

        headerList.add(contentHeader);
        headerCounter++;

    }

    // Add size header, if size is available
    // (as of now -- for local files only).

    if (fileDownloadObject.isFile()) {
        long fileSize = fileDownloadObject.getSize();

        if (fileSize > 0) {
            // Don't forget about the variable header we are adding to the
            // TAB files. This will change the size of the content!

            if (fileDownloadObject.getVarHeader() != null && (!fileDownloadObject.noVarHeader())) {
                fileSize += fileDownloadObject.getVarHeader().length();
            }
            Header contentLengthHeader = new Header();
            contentLengthHeader.setName("Content-Length");
            contentLengthHeader.setValue((new Long(fileSize)).toString());

            headerList.add(contentLengthHeader);
            headerCounter++;
        }
    }

    if (headerCounter > 0) {
        Header[] respHeaders = new Header[headerCounter];
        for (int i = 0; i < headerCounter; i++) {
            respHeaders[i] = headerList.get(i);
        }
        fileDownloadObject.setResponseHeaders(respHeaders);
    }

}

From source file:com.intuit.tank.http.BaseRequestHandlerTest.java

/**
 * Run the void processResponse(byte[],long,long,BaseResponse,String,int,Header[],HttpState) method test.
 *
 * @throws Exception/*from www. j a  v a  2s.  c om*/
 *
 * @generatedBy CodePro at 12/16/14 3:57 PM
 */
@Test
public void testProcessResponse_10() throws Exception {
    byte[] bResponse = new byte[] {};
    long startTime = 1L;
    long endTime = 1L;
    BaseResponse response = new BinaryResponse();
    String message = "";
    int httpCode = 1;
    Header[] headers = new Header[] { new Header() };
    HttpState httpstate = new HttpState();

    BaseRequestHandler.processResponse(bResponse, startTime, endTime, response, message, httpCode, headers,
            httpstate);

    // An unexpected exception was thrown in user code while executing this test:
    //    java.lang.NoClassDefFoundError: Could not initialize class com.intuit.tank.http.binary.BinaryResponse
}

From source file:com.intuit.tank.http.BaseRequestHandlerTest.java

/**
 * Run the void processResponse(byte[],long,long,BaseResponse,String,int,Header[],HttpState) method test.
 *
 * @throws Exception/*ww w. ja  v a2s .  co m*/
 *
 * @generatedBy CodePro at 12/16/14 3:57 PM
 */
@Test
public void testProcessResponse_14() throws Exception {
    byte[] bResponse = new byte[] {};
    long startTime = 1L;
    long endTime = 1L;
    BaseResponse response = new BinaryResponse();
    String message = "";
    int httpCode = 1;
    Header[] headers = new Header[] { new Header() };
    HttpState httpstate = new HttpState();

    BaseRequestHandler.processResponse(bResponse, startTime, endTime, response, message, httpCode, headers,
            httpstate);

    // An unexpected exception was thrown in user code while executing this test:
    //    java.lang.NoClassDefFoundError: Could not initialize class com.intuit.tank.http.binary.BinaryResponse
}

From source file:org.apache.axis2.client.Stub.java

/**
 * add an http header with name and value to message context
 *
 * @param messageContext/*from w  w w.  java 2  s .com*/
 * @param name
 * @param value
 */
protected void addHttpHeader(MessageContext messageContext, String name, String value) {
    java.lang.Object headersObj = messageContext.getProperty(HTTPConstants.HTTP_HEADERS);
    if (headersObj == null) {
        headersObj = new java.util.ArrayList();
    }
    java.util.List headers = (java.util.List) headersObj;
    Header header = new Header();
    header.setName(name);
    header.setValue(value);
    headers.add(header);
    messageContext.setProperty(HTTPConstants.HTTP_HEADERS, headers);
}