Package com.onpositive.google.scrapbook

Source Code of com.onpositive.google.scrapbook.ConsoleServletCommunicatorFactory

package com.onpositive.google.scrapbook;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.params.HttpMethodParams;
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;
import com.onpositive.gae.tools.license.BadUserIdException;

public class ConsoleServletCommunicatorFactory {

  public static IConsoleServletCommunicator getCommunicator() {
    return new IConsoleServletCommunicator() {

      // "http://localhost:45463
     
      public String evaluate(final byte[] content, IJavaProject project,
          boolean isDebug) {
        String connectionString = null;
        if (isDebug) {
          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 + "/";
        }
        String uri = null;
        String appId = GaeBridge.getAppId(project.getProject());
        String version = GaeBridge.getVersion(project.getProject());
        HttpClient httpClient = new HttpClient();
        if (connectionString == null) {
          if (appId == null) {
            Display.getDefault().syncExec(new Runnable() {

             
              public void run() {
                MessageDialog
                    .openError(Display.getCurrent()
                        .getActiveShell(), "Error",
                        "Current Project is not Google App Engine Project");
              }
            });
            return "";
          }
          if (appId.length() == 0) {
            Display.getDefault().syncExec(new Runnable() {

             
              public void run() {
                MessageDialog
                    .openError(Display.getCurrent()
                        .getActiveShell(), "Error",
                        "Current Project has not application id, please setup application id and try again.");
              }
            });
            return "";
          }
          uri = "http://" + version + ".latest." + appId
              + ".appspot.com/appwrench/console";
        } else {
          uri = connectionString + "appwrench/console";
        }
        PostMethod postMethod = new PostMethod(uri);
        String sessionCode = "";
        try {
          sessionCode = SessionManager.getSessionCode(appId, isDebug);
        } catch (BadUserIdException e1) {
          Display.getDefault().syncExec(new Runnable() {

           
            public void run() {
              MessageDialog
                  .openError(Display.getCurrent()
                      .getActiveShell(), "Error",
                      "Bad user id");
            }
          });
          return "";
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
          DataOutputStream doc = new DataOutputStream(out);
          if (sessionCode != null) {
            doc.writeUTF(sessionCode);

          } else {
            doc.writeUTF("");
          }
          doc.writeInt(content.length);
          doc.write(content);
        } catch (Exception e) {
          e.printStackTrace();
        }
        final byte[] toTransfer = out.toByteArray();
        postMethod.setRequestEntity(new MultipartRequestEntity(
            new Part[] { new Part() {

             
              public String getCharSet() {
                return CHARSET;
              }

             
              public String getContentType() {
                return "class";
              }

             
              public String getName() {
                return "item";
              }

             
              public String getTransferEncoding() {
                return CHARSET;
              }

             
              protected long lengthOfData() throws IOException {
                return toTransfer.length;
              }

             
              protected void sendData(OutputStream out)
                  throws IOException {
                out.write(toTransfer);
              }

            } }, new HttpMethodParams()));
        try {

          int executeMethod = httpClient.executeMethod(postMethod);
          if (executeMethod == 404) {

            Display.getDefault().syncExec(new Runnable() {

             
              public void run() {
                MessageDialog
                    .openError(Display.getCurrent()
                        .getActiveShell(), "Error",
                        "Java Console is not deployed to target server");
              }
            });
            return "";
          }
          DataInputStream ds = new DataInputStream(postMethod
              .getResponseBodyAsStream());
          int code = ds.readInt();
          if (code != 200) {
            if (code == 500) {
              boolean estabilishSession = SessionManager
                  .estabilishSession(appId, version);
              if (estabilishSession) {
                return evaluate(content, project, isDebug);
              }
              Display.getDefault().syncExec(new Runnable() {

               
                public void run() {
                  MessageDialog
                      .openError(Display.getCurrent()
                          .getActiveShell(), "Error",
                          "Not authorized to access this application.");
                }
              });             
              return "";
            }
          }
          String responseBody = ds.readUTF();
          return "<pre>" + responseBody + "</pre>";
        } catch (HttpException e) {
          return e.getMessage();
        } catch (IOException e) {
          return e.getMessage();
        }
      }
    };

  };
}
TOP

Related Classes of com.onpositive.google.scrapbook.ConsoleServletCommunicatorFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.