Package org.uiautomation.ios.inspector

Source Code of org.uiautomation.ios.inspector.IDEServlet

/*
* Copyright 2012-2013 eBay Software Foundation and ios-driver committers
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.uiautomation.ios.inspector;

import org.uiautomation.ios.IOSServer;
import org.uiautomation.ios.IOSServerManager;
import org.uiautomation.ios.inspector.controllers.IDECommandController;
import org.uiautomation.ios.inspector.controllers.IDEController;
import org.uiautomation.ios.inspector.controllers.NotImplementedIDEController;
import org.uiautomation.ios.inspector.controllers.RefreshController;
import org.uiautomation.ios.inspector.controllers.ResourceController;
import org.uiautomation.ios.inspector.controllers.SessionGuesserController;
import org.uiautomation.ios.inspector.controllers.TreeController;
import org.uiautomation.ios.inspector.controllers.WebViewContentController;
import org.uiautomation.ios.inspector.model.Cache;
import org.uiautomation.ios.inspector.model.CacheDefaultImpl;
import org.uiautomation.ios.inspector.views.View;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IDEServlet extends HttpServlet {

  private static final Logger log = Logger.getLogger(IDEServlet.class.getName());
  // TODO freynaud
  String path;
  String url;
  // JSONObject cache = null;
  private final Model model = new Model();

  private static final long serialVersionUID = 333974658353413397L;

  private final List<IDECommandController> controllers = new ArrayList<>();

  @Override
  public void init() throws ServletException {
    super.init();
    URL u = null;

    try {
      IOSServerManager
          driver =
          (IOSServerManager) getServletContext().getAttribute(IOSServer.DRIVER);
      if (driver != null) {
        int port = driver.getPort();
        u = new URL(System.getProperty("endpoint", "http://localhost:" + port + "/wd/hub"));
      } else {
        log.warning("couldn't find the end point.");
      }
    } catch (MalformedURLException e) {
      log.log(Level.SEVERE, "url format", e);
    }
    Cache cache = (Cache) getServletContext().getAttribute(Cache.KEY);
    if (cache == null) {
      cache = new CacheDefaultImpl(u);
    }

    controllers.add(new SessionGuesserController(cache));
    controllers.add(new IDEController(cache));
    controllers.add(new ResourceController(cache));
    controllers.add(new RefreshController(cache));
    controllers.add(new TreeController(cache));
    controllers.add(new WebViewContentController(cache));
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse response) throws IOException {
    response.setCharacterEncoding("UTF-8");
    try {
      IDECommandController controller = getController(req.getPathInfo());
      View view = controller.handle(req);
      view.render(response);
    } catch (Exception e) {
      log.log(Level.SEVERE, "ide process error", e);
    }
  }

  private IDECommandController getController(String pathInfo) {
    for (IDECommandController c : controllers) {
      if (c.canHandle(pathInfo)) {
        return c;
      }
    }
    return new NotImplementedIDEController();
  }
}
TOP

Related Classes of org.uiautomation.ios.inspector.IDEServlet

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.