Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2013 eBay Software Foundation and selendroid 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 io.selendroid.server;
 
 
 import java.util.Map;
 
 
 public class SelendroidServlet extends BaseServlet {
   private static final Logger log = Logger.getLogger(SelendroidServlet.class.getName());
       new HashMap<StringBaseRequestHandler>();
 
     this. = driver;
     this. = conf;
     init();
   }
 
   protected void init() {
     register(new CreateSessionHandler("/wd/hub/session"));
     register(new ListSessionsHandler("/wd/hub/sessions"));
     register(new GetCapabilities("/wd/hub/session/:sessionId"));
 
     register(new GetLogTypes("/wd/hub/session/:sessionId/log/types"));
     register(new GetLogs("/wd/hub/session/:sessionId/log"));
     if (!.isDeviceScreenshot()) {
       register(new CaptureScreenshot("/wd/hub/session/:sessionId/screenshot"));
     } // otherwise the request will be automatically forwarded to the device
 
 
     register(new InspectorTreeHandler("/inspector/session/:sessionId/tree"));
     register(new InspectorScreenshotHandler("/inspector/session/:sessionId/screenshot"));
     register(new InspectorUiHandler("/inspector/session/:sessionId"));
     register(new DeleteSessionHandler("/wd/hub/session/:sessionId"));
     register(new RequestRedirectHandler("/wd/hub/session/"));
 
     register(new GetLogs("/wd/hub/session/:sessionId/log"));
     register(new AdbSendKeyEvent("/wd/hub/-selendroid/:sessionId/adb/sendKeyEvent"));
     register(new AdbSendText("/wd/hub/-selendroid/:sessionId/adb/sendText"));
     register(new AdbTap("/wd/hub/-selendroid/:sessionId/adb/tap"));
         "/wd/hub/-selendroid/:sessionId/adb/executeShellCommand"));
   }
 
   @Override
   public void handleRequest(HttpRequest requestHttpResponse response,
       BaseRequestHandler foundHandler) {
     BaseRequestHandler handler = null;
     if ("/favicon.ico".equals(request.uri()) && foundHandler == null) {
       response.setStatus(404);
       response.end();
       return;
     }
     if ("/inspector/".equals(request.uri()) || "/inspector".equals(request.uri())) {
       if (.getActiveSessions().isEmpty()) {
         response.setStatus(200);
         response
             .setContent("Selendroid inspector can only be used if there is an active test session running. "
                 + "To start a test session, add a break point into your test code and run the test in debug mode.");
         response.end();
         return;
       } else {
         // response.setStatus(302);
         String session = .getActiveSessions().get(0).getSessionKey();
        String newSessionUri =
            "http://" + request.header("Host") + "/inspector/session/" + session + "/";
        .info("new Inspector URL: " + newSessionUri);
        response.sendTemporaryRedirect(newSessionUri);
        response.end();
        return;
      }
    }
    if (foundHandler == null) {
      if (!.isEmpty()) {
        // trying to find an redirect handler
        for (Map.Entry<StringBaseRequestHandlerentry : .entrySet()) {
          if (request.uri().startsWith(entry.getKey())) {
            String sessionId =
                getParameter("/wd/hub/session/:sessionId"request.uri(), ":sessionId"false);
            handler = entry.getValue();
            if (.isValidSession(sessionId)) {
              request.data().put(sessionId);
            }
          }
        }
      }
      if (handler == null) {
        response.setStatus(404);
        response.end();
        return;
      }
    } else {
      handler = foundHandler;
    }
    String sessionId = getParameter(handler.getMappedUri(), request.uri(), ":sessionId");
    if (sessionId != null) {
      request.data().put(sessionId);
    }
    request.data().put();
    Response result;
    try {
      result = handler.handle(request);
    } catch (Exception e) {
      e.printStackTrace();
      .severe("Error occurred while handling request: " + e.fillInStackTrace());
      replyWithServerError(response);
      return;
    }
    if (result instanceof SelendroidResponse) {
      handleResponse(requestresponse, (SelendroidResponseresult);
    } else if (result instanceof JsResult) {
      JsResult js = (JsResultresult;
      response.setContentType("application/x-javascript");
      response.setEncoding(Charset.forName("UTF-8"));
      response.setContent(js.render());
      response.end();
    } else {
      UiResponse uiResponse = (UiResponseresult;
      response.setEncoding(Charset.forName("UTF-8"));
      response.setStatus(200);
      if (uiResponse != null) {
        if (uiResponse.getObject() instanceof byte[]) {
          response.setContentType("image/png");
          byte[] data = (byte[]) uiResponse.getObject();
          response.setContent(data);
        } else {
          response.setContentType("text/html");
          String resultString = uiResponse.render();
          response.setContent(resultString);
        }
      }
      response.end();
    }
  }
New to GrepCode? Check out our FAQ X