es.uned.dia.jcsombria.model_elements.softwarelinks.nodejs.HttpTransport.java Source code

Java tutorial

Introduction

Here is the source code for es.uned.dia.jcsombria.model_elements.softwarelinks.nodejs.HttpTransport.java

Source

/**
 * HttpTransport
 * author: Jess Chacn <jcsombria@gmail.com>
 *
 * Copyright (C) 2013 Jess Chacn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package es.uned.dia.jcsombria.model_elements.softwarelinks.nodejs;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpTransport implements Transport {
    private URL serverURL;

    public HttpTransport(String url) throws MalformedURLException {
        setServerURL(url);
    }

    public void setServerURL(URL url) {
        serverURL = url;
    }

    public void setServerURL(String url) throws MalformedURLException {
        serverURL = new URL(url);
    }

    public Object send(String request) throws Exception {
        Object response = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httppost = new HttpPost(serverURL.toString());
            System.out.println("Executing request " + httppost.getRequestLine());
            System.out.println(request);
            httppost.setEntity(new StringEntity(request));
            // Create a custom response handler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
                public String handleResponse(final HttpResponse response)
                        throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }
            };
            String responseBody = httpclient.execute(httppost, responseHandler);
            response = responseBody;
        } finally {
            httpclient.close();
        }
        return response;
    }
}