package com.onpositive.gae.profiler.core;
import java.io.DataInputStream;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import com.onpositive.gae.tools.GaeBridge;
import com.onpositive.gae.tools.SessionManager;
import com.onpositive.gae.tools.core.CheckLaunchJob;
import com.onpositive.gae.tools.core.DebugServerLaunchManager;
public class ProfilerCommunicator {
public static InputStream requestCurrentPerformanceData(
IJavaProject project, boolean debug) throws Exception {
HttpClient httpClient = new HttpClient();
String uri = getUri(project, debug);
String sessionCode = SessionManager.getSessionCode(GaeBridge
.getAppId(project.getProject()),debug);
if (sessionCode==null){
sessionCode="";
}
PostMethod method = new PostMethod(uri);
method.setParameter("reset", "false");
method.setParameter("auth", sessionCode);
httpClient.executeMethod(method);
if (method.getStatusCode()==404){
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog
.openError(
Display.getCurrent().getActiveShell(),
"Error",
"AppWrench Profiler is not deployed to target server");
}
});
}
InputStream responseBodyAsStream = method.getResponseBodyAsStream();
DataInputStream dataInputStream = new DataInputStream(responseBodyAsStream);
int readInt = dataInputStream.readInt();
if (readInt==200){
return dataInputStream;
}
if (readInt==500){
boolean estabilishSession = SessionManager.estabilishSession(GaeBridge.getAppId(project
.getProject()), GaeBridge.getVersion(project.getProject()));
if (estabilishSession){
return requestCurrentPerformanceData(project, debug);
}
}
return null;
}
public static boolean requestClearPerformanceData(IJavaProject project,
boolean debug) throws Exception {
HttpClient httpClient = new HttpClient();
String uri = getUri(project, debug);
PostMethod method = new PostMethod(uri);
method.setParameter("reset", "true");
String sessionCode = SessionManager.getSessionCode(GaeBridge
.getAppId(project.getProject()),debug);
if (sessionCode==null){
sessionCode="";
}
method.setParameter("auth", sessionCode);
httpClient.executeMethod(method);
DataInputStream st = new DataInputStream(method
.getResponseBodyAsStream());
int res = st.readInt();
if (res == 200) {
return true;
}
if (res == 500) {
boolean estabilishSession = SessionManager.estabilishSession(GaeBridge.getAppId(project
.getProject()), GaeBridge.getVersion(project.getProject()));
if (estabilishSession){
return requestClearPerformanceData(project, debug);
}
}
return false;
}
private static String getUri(IJavaProject project, boolean debug) {
String connectionString;
if (debug) {
int port = DebugServerLaunchManager.getPort(project);
if (port == -1) {
CheckLaunchJob checkLaunchJob = new CheckLaunchJob(
"Checking Launch", project, true);
try {
checkLaunchJob.schedule();
checkLaunchJob.join();
} catch (InterruptedException e) {
}
port = DebugServerLaunchManager.getPort(project);
}
connectionString = "http://localhost:" + port + "/";
} else {
String appId = GaeBridge.getAppId(project.getProject());
String version = GaeBridge.getVersion(project.getProject());
if (appId == null || version == null) {
throw new RuntimeException();
}
connectionString = "http://" + version + ".latest." + appId
+ ".appspot.com/";
}
return connectionString + "appwrench/profiler";
}
}