Search in sources :

Example 1 with NotYetImplementedException

use of io.appium.espressoserver.lib.handlers.exceptions.NotYetImplementedException in project appium-espresso-driver by appium.

the class Router method route.

@SuppressWarnings("unchecked")
public BaseResponse route(IHTTPSession session) {
    String uri = session.getUri();
    Method method = session.getMethod();
    // Look for a route that matches this URL
    RouteDefinition matchingRoute = routeMap.findMatchingRoute(method, uri);
    // If no route found, return a 404 Error Response
    if (matchingRoute == null) {
        return new ErrorResponse(NanoHTTPD.Response.Status.NOT_FOUND, String.format("No such route %s", uri));
    }
    // Get the handler, parameter class and URI parameters
    RequestHandler handler = matchingRoute.getHandler();
    Class<? extends AppiumParams> paramClass = matchingRoute.getParamClass();
    Map<String, String> uriParams = matchingRoute.getUriParams(uri);
    // Parse the appium params
    String postJson;
    try {
        postJson = parseBody(session);
    } catch (IOException e) {
        return new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, e.getMessage());
    } catch (NanoHTTPD.ResponseException e) {
        return new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, e.getMessage());
    }
    AppiumParams appiumParams;
    if (postJson == null) {
        appiumParams = new AppiumParams();
    } else {
        appiumParams = paramClass.cast((new Gson()).fromJson(postJson, paramClass));
    }
    appiumParams.setSessionId(uriParams.get("sessionId"));
    appiumParams.setElementId(uriParams.get("elementId"));
    // Validate the sessionId
    if (appiumParams.getSessionId() != null && !appiumParams.getSessionId().equals(Session.getGlobalSession().getId())) {
        return new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, "Invalid session ID " + appiumParams.getSessionId());
    }
    // Create the result
    try {
        Object handlerResult = handler.handle(appiumParams);
        String sessionId = appiumParams.getSessionId();
        // If it's a new session, pull out the newly created Session ID
        if (handlerResult != null && handlerResult.getClass() == Session.class) {
            sessionId = ((Session) handlerResult).getId();
        }
        AppiumResponse appiumResponse = new AppiumResponse<>(AppiumStatus.SUCCESS, handlerResult, sessionId);
        System.out.println("Finished processing " + method + " request for '" + uri + "'");
        return appiumResponse;
    } catch (NoSuchElementException e) {
        return new AppiumResponse<>(AppiumStatus.NO_SUCH_ELEMENT, e.getMessage());
    } catch (SessionNotCreatedException e) {
        return new AppiumResponse<>(AppiumStatus.SESSION_NOT_CREATED_EXCEPTION, e.getMessage());
    } catch (InvalidStrategyException e) {
        return new AppiumResponse<>(AppiumStatus.INVALID_SELECTOR, e.getMessage());
    } catch (MissingCommandsException e) {
        return new ErrorResponse(NanoHTTPD.Response.Status.NOT_FOUND, e.getMessage());
    } catch (NotYetImplementedException e) {
        return new ErrorResponse(NanoHTTPD.Response.Status.NOT_IMPLEMENTED, e.getMessage());
    } catch (AppiumException e) {
        return new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, e.getMessage());
    }
}
Also used : MissingCommandsException(io.appium.espressoserver.lib.handlers.exceptions.MissingCommandsException) SessionNotCreatedException(io.appium.espressoserver.lib.handlers.exceptions.SessionNotCreatedException) NanoHTTPD(fi.iki.elonen.NanoHTTPD) Gson(com.google.gson.Gson) Method(fi.iki.elonen.NanoHTTPD.Method) IOException(java.io.IOException) ErrorResponse(io.appium.espressoserver.lib.http.response.ErrorResponse) AppiumResponse(io.appium.espressoserver.lib.http.response.AppiumResponse) NotYetImplementedException(io.appium.espressoserver.lib.handlers.exceptions.NotYetImplementedException) RequestHandler(io.appium.espressoserver.lib.handlers.RequestHandler) AppiumException(io.appium.espressoserver.lib.handlers.exceptions.AppiumException) AppiumParams(io.appium.espressoserver.lib.model.AppiumParams) NoSuchElementException(io.appium.espressoserver.lib.handlers.exceptions.NoSuchElementException) InvalidStrategyException(io.appium.espressoserver.lib.handlers.exceptions.InvalidStrategyException) CreateSession(io.appium.espressoserver.lib.handlers.CreateSession) IHTTPSession(fi.iki.elonen.NanoHTTPD.IHTTPSession) DeleteSession(io.appium.espressoserver.lib.handlers.DeleteSession) Session(io.appium.espressoserver.lib.model.Session) GetSession(io.appium.espressoserver.lib.handlers.GetSession)

Aggregations

Gson (com.google.gson.Gson)1 NanoHTTPD (fi.iki.elonen.NanoHTTPD)1 IHTTPSession (fi.iki.elonen.NanoHTTPD.IHTTPSession)1 Method (fi.iki.elonen.NanoHTTPD.Method)1 CreateSession (io.appium.espressoserver.lib.handlers.CreateSession)1 DeleteSession (io.appium.espressoserver.lib.handlers.DeleteSession)1 GetSession (io.appium.espressoserver.lib.handlers.GetSession)1 RequestHandler (io.appium.espressoserver.lib.handlers.RequestHandler)1 AppiumException (io.appium.espressoserver.lib.handlers.exceptions.AppiumException)1 InvalidStrategyException (io.appium.espressoserver.lib.handlers.exceptions.InvalidStrategyException)1 MissingCommandsException (io.appium.espressoserver.lib.handlers.exceptions.MissingCommandsException)1 NoSuchElementException (io.appium.espressoserver.lib.handlers.exceptions.NoSuchElementException)1 NotYetImplementedException (io.appium.espressoserver.lib.handlers.exceptions.NotYetImplementedException)1 SessionNotCreatedException (io.appium.espressoserver.lib.handlers.exceptions.SessionNotCreatedException)1 AppiumResponse (io.appium.espressoserver.lib.http.response.AppiumResponse)1 ErrorResponse (io.appium.espressoserver.lib.http.response.ErrorResponse)1 AppiumParams (io.appium.espressoserver.lib.model.AppiumParams)1 Session (io.appium.espressoserver.lib.model.Session)1 IOException (java.io.IOException)1