Skip to content

Commit

Permalink
Refactor httpClient class utility
Browse files Browse the repository at this point in the history
  • Loading branch information
eljoujat committed Feb 4, 2016
1 parent bd6e02e commit cbe8cc4
Showing 1 changed file with 10 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -35,7 +32,6 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.Status;
import org.eclipseplugins.impexeditor.core.Activator;
Expand All @@ -47,7 +43,6 @@

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;


public class ImpexHttpClient
Expand All @@ -56,37 +51,15 @@ public class ImpexHttpClient
private static final String USER_AGENT = "Mozilla/5.0";
private final static Pattern pattern = Pattern
.compile("JSESSIONID=[a-zA-Z0-9_-]*");
private String jsessionId;
private String JSESSIONID;
private final String hostName;
private final ILog logger = Activator.getDefault().getLog();
private static final ILog logger = Activator.getDefault().getLog();

public ImpexHttpClient(final String hostName)
{
this.hostName = hostName;
}

public static void main(final String[] args) throws Exception
{

final ImpexHttpClient impexHttpClient = new ImpexHttpClient("http://localhost:9001/hac");
final long startTime = System.currentTimeMillis();
final Map<String, JsonArray> allatypes = new HashMap<String, JsonArray>();
for (final JsonValue type : impexHttpClient.getAllTypes())
{
allatypes.put(type.asString(), impexHttpClient.getTypeandAttribute(type.asString()).get("attributes").asArray());
}
for (final String type : allatypes.keySet())
{
System.out.println("type :" + type);
for (final JsonValue string : allatypes.get(type))
{
System.out.println("---- " + string.asString());
}
}
final long stopTime = System.currentTimeMillis();
final long elapsedTime = stopTime - startTime;
System.out.println("Success elapsed time in seconde " + TimeUnit.MILLISECONDS.toSeconds(elapsedTime));
}

public JsonObject getTypeandAttribute(final String type) throws Exception
{
Expand Down Expand Up @@ -123,7 +96,6 @@ public JsonArray getAllTypes() throws Exception
private String sendLoginPost()
{

final String jssessionId = null;
final String validJSessionID = getValidJSessionID();
String csrfToken = null;
try
Expand All @@ -141,7 +113,6 @@ private String sendLoginPost()
new BasicNameValuePair("j_username", "admin"),
new BasicNameValuePair("j_password", "nimda"),
new BasicNameValuePair("_csrf", csrfToken) });
final String jsessionIdCookieToken = validJSessionID;
HttpResponse response = null;
String jsessionIDToken = null;
try
Expand Down Expand Up @@ -169,22 +140,21 @@ private String sendLoginPost()
}
catch (final IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
logger.log(new Status(Status.ERROR, Activator.PLUGIN_ID, e!=null?e.getMessage():"IO Exception"));
}
return null;
}

public String getJsessionId()
{
if (jsessionId != null)
if (JSESSIONID != null)
{
return jsessionId;
return JSESSIONID;
}
else
{
jsessionId = sendLoginPost();
return jsessionId;
JSESSIONID = sendLoginPost();
return JSESSIONID;
}
}

Expand Down Expand Up @@ -231,19 +201,14 @@ public String validateImpex(final String content)
new BasicNameValuePair("scriptContent", content), new BasicNameValuePair("validationEnum", "IMPORT_STRICT"),
new BasicNameValuePair("encoding", "UTF-8"),
new BasicNameValuePair("maxThreads", "4") });
System.out.println("############################# ");
System.out.println(content);
System.out.println("############################# ");
try
{
final HttpResponse response = makeHttpPostRequest(hostName + "/console/impex/import", getJsessionId(), params);
final String html = EntityUtils.toString(response.getEntity());
System.out.println(html);
//TODO parse html ? or better idea execute a remote groovey script wich will restun a json reult easy to parse
}
catch (final IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
logger.log(new Status(Status.ERROR, Activator.PLUGIN_ID, Status.ERROR, "Connection refused to " + hostName, e));
}
return "";

Expand Down Expand Up @@ -272,7 +237,7 @@ private String getValidJSessionID()
}
catch (final IOException e)
{
e.printStackTrace();
logger.log(new Status(Status.ERROR, Activator.PLUGIN_ID, Status.ERROR, "Connection refused to " + hostName, e));
}
if (res == null)
{
Expand Down

0 comments on commit cbe8cc4

Please sign in to comment.