src/org/myorg/myscclient/SCWSClient.java
package org.myorg.myscclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

/**
 * A sample RESTful webservice client which uses the Apache HTTP Client module
 * to invoke the sample restful service.
 * The webservice address is hardcoded in the LOCAL_HOST_ADDRESS field.
 */
public class SCWSClient {

    private static final Logger LOG = Logger.getLogger(SCWSClient.class.getName());

    // The RESTful web service address
    private static final String LOCAL_HOST_ADDRESS = "http://localhost:8084/MySCServer/resources/validator/validate/"; //NOI18N

    public String validate(String sdfInput) throws ServiceLevelException, HTTPLevelErrorException, IOException {
        HttpParams httpParams = new BasicHttpParams();
        HttpClient client = null;
        String responseString = null;
        HttpConnectionParams.setConnectionTimeout(httpParams, 25000);
        HttpConnectionParams.setSoTimeout(httpParams, 25000);
        HttpEntity responseEntity = null;

        StringBody b = new StringBody(sdfInput);
        HttpPost servicePost = new HttpPost(LOCAL_HOST_ADDRESS);
        servicePost.getParams().setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
        try {
            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("SDF Part", b); // NOI18N
            servicePost.setEntity(reqEntity);
            client = new DefaultHttpClient(httpParams);
            HttpResponse response = client.execute(servicePost);
            responseEntity = response.getEntity();
            responseString = getString(responseEntity.getContent());

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return responseString;
            } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_ACCEPTABLE) {
                // dealing with service level exceptions
                //something went wrong and server didnt send the proper response.
                // for example service cannot be found or internal server error...
                LOG.log(Level.INFO, "Service Invocation failed because: {0} : {1}", //NOI18N
                        new Object[]{response.getStatusLine(), response.getStatusLine().getReasonPhrase()}); //NOI18N
                throw new ServiceLevelException(responseString);
            } else { // dealing with other http level errors
                // dealing with service level exceptions
                // something went wrong and server didnt send the proper response.
                LOG.log(Level.INFO, "Service Invocation failed because: {0} : {1}", //NOI18N
                        new Object[]{response.getStatusLine(), response.getStatusLine().getReasonPhrase()}); //NOI18N
                throw new HTTPLevelErrorException(response);
            }

        } finally {
            //release the connection to the manager
            if (client != null) {
                client.getConnectionManager().shutdown();
            }
        }
    }

    /**
     * Take an InputStream and turn it into String.
     * @param is the stream we want to convert
     * @return the content of the stream as String, if input is null output will be null as well.
     * @throws Exception
     */
    private String getString(InputStream is) throws IOException {
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    }
}