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;
public class SCWSClient {
private static final Logger LOG = Logger.getLogger(SCWSClient.class.getName());
private static final String LOCAL_HOST_ADDRESS = "http://localhost:8084/MySCServer/resources/validator/validate/";
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);
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) {
LOG.log(Level.INFO, "Service Invocation failed because: {0} : {1}",
new Object[]{response.getStatusLine(), response.getStatusLine().getReasonPhrase()});
throw new ServiceLevelException(responseString);
} else {
LOG.log(Level.INFO, "Service Invocation failed because: {0} : {1}",
new Object[]{response.getStatusLine(), response.getStatusLine().getReasonPhrase()});
throw new HTTPLevelErrorException(response);
}
} finally {
if (client != null) {
client.getConnectionManager().shutdown();
}
}
}
@param
@return
@throws
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();
}
}