org.cryptable.pki.communication.http.PKIHTTPCommunication.java Source code

Java tutorial

Introduction

Here is the source code for org.cryptable.pki.communication.http.PKIHTTPCommunication.java

Source

/**
 * The MIT License (MIT)
 *
 * Copyright (c) <2013> <Cryptable>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
package org.cryptable.pki.communication.http;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
import java.io.InputStream;

/**
 * The Communication class to support HTTP
 *
 * Created with IntelliJ IDEA.
 * User: davidtillemans
 * Date: 8/06/13
 * Time: 08:31
 * To change this template use File | Settings | File Templates.
 */
public class PKIHTTPCommunication {

    private HttpClient httpClient;

    private HttpPost httpPost;

    public HttpClient getHttpClient() {
        return httpClient;
    }

    public void setHttpClient(HttpClient httpClient) {
        this.httpClient = httpClient;
    }

    public HttpPost getHttpPost() {
        return httpPost;
    }

    public void setHttpPost(HttpPost httpPost) {
        this.httpPost = httpPost;
    }

    public PKIHTTPCommunication(String uri) {
        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(uri);
    }

    public byte[] sendReceiveMsg(byte[] msg) throws IOException {

        httpPost.addHeader(URLEncodedUtils.CONTENT_TYPE, "application/pkixcmp");
        httpPost.setEntity(new ByteArrayEntity(msg));
        HttpResponse httpResponse = httpClient.execute(httpPost);

        InputStream inputStream = httpResponse.getEntity().getContent();
        byte[] byteResponse = new byte[(int) httpResponse.getEntity().getContentLength()];
        inputStream.read(byteResponse);
        return byteResponse;
    }
}