package collabreview.intellij;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.io.IOException;
/*
* Created by IntelliJ IDEA.
* User: Admin-Dencheva
* Date: 24.07.2008
* Time: 16:29:07
* To change this template use File | Settings | File Templates.
*/
public class CollabreviewClient {
private static String url = "https://hydra.fit.fraunhofer.de/collabreview/service";
private static HttpClient client = new HttpClient();
public static void get(String command, String account, char[] password, String document){
// Create a method GET instance.
GetMethod methodGET = new GetMethod(url);
// Provide custom retry handler is necessary
methodGET.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
// Set the parameters of the method GET
methodGET.getParams().setParameter("command", command);
methodGET.getParams().setParameter("account", account);
methodGET.getParams().setParameter("password", password);
methodGET.getParams().setParameter("document", document);
try {
// Execute the method GET.
int statusCode = client.executeMethod(methodGET);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method GET failed: " + methodGET.getStatusLine());
}
// Read the response body.
byte[] responseBody = methodGET.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation by method GET: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error by method GET: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
methodGET.releaseConnection();
}
}
public static void post(String command, String account, char[] password, String document,
String text, int rating, boolean anonymous){
// Create a method GET instance.
PostMethod methodPOST = new PostMethod(url);
// Set methodPOST parameters.
methodPOST.getParams().setParameter("command", command);
methodPOST.getParams().setParameter("account", account);
methodPOST.getParams().setParameter("password", password);
methodPOST.getParams().setParameter("document", document);
methodPOST.getParams().setParameter("text", text);
methodPOST.getParams().setParameter("rating", rating);
methodPOST.getParams().setParameter("anonymous", anonymous);
// Provide custom retry handler is necessary
methodPOST.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method POST.
int statusCode = client.executeMethod(methodPOST);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method POST failed: " + methodPOST.getStatusLine());
}
// Read the response body of method POST.
byte[] responseBody = methodPOST.getResponseBody();
//methodPOST.addParameter("command");
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation by method POST: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error by method POST: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
methodPOST.releaseConnection();
}
}
/*
public static void main(String[] args) {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Set client parameters.
/* client.getParams().setParameter("http.command", "command");
client.getParams().setParameter("http.account", "account");
client.getParams().setParameter("http.password", "password");
client.getParams().setParameter("http.document", "document");
client.getParams().setParameter("http.text", "text");
client.getParams().setParameter("http.rating", "rating");
client.getParams().setParameter("http.anonymous", "anonymous"); /
/////////////////////////////////////////////////////////////////////////////
///////////////////////// GET method instance ///////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Create a method GET instance.
GetMethod methodGET = new GetMethod(url);
/* Set GET method parameters.
methodGET.getParams().setParameter("http.command", "command");
methodGET.getParams().setParameter("http.account", "account");
methodGET.getParams().setParameter("http.password", "password");
methodGET.getParams().setParameter("http.document", "document"); /
// Provide custom retry handler is necessary
methodGET.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method GET.
int statusCode = client.executeMethod(methodGET);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method GET failed: " + methodGET.getStatusLine());
}
// Read the response body.
byte[] responseBody = methodGET.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation by method GET: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error by method GET: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
methodGET.releaseConnection();
}
///////////////////////// end GET method instance//////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////// POST method instance ////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Create a method GET instance.
PostMethod methodPOST = new PostMethod(url);
/* Set methodPOST parameters.
methodPOST.getParams().setParameter("http.command", "command");
methodPOST.getParams().setParameter("http.account", "account");
methodPOST.getParams().setParameter("http.password", "password");
methodPOST.getParams().setParameter("http.document", "document");
methodPOST.getParams().setParameter("http.text", "text");
methodPOST.getParams().setParameter("http.rating", "rating");
methodPOST.getParams().setParameter("http.anonymous", "anonymous"); /
// Provide custom retry handler is necessary
methodPOST.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method POST.
int statusCode = client.executeMethod(methodPOST);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method POST failed: " + methodPOST.getStatusLine());
}
// Read the response body of method POST.
byte[] responseBody = methodPOST.getResponseBody();
//methodPOST.addParameter("command");
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation by method POST: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error by method POST: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
methodPOST.releaseConnection();
}
///////////////////////// end POST method instance//////////////////////////////////
} */
}