All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.selendroid.server.SelendroidResponse Maven / Gradle / Ivy

There is a newer version: 0.17.0
Show newest version
/*
 * 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 org.json.JSONException;
import org.json.JSONObject;

import java.io.PrintWriter;
import java.io.StringWriter;

public class SelendroidResponse implements Response {
    private String sessionId;
    private int status;
    private Object value;

    protected SelendroidResponse() {
    }

    public SelendroidResponse(String sessionId, int status, JSONObject value) {
        this.sessionId = sessionId;
        this.status = status;
        this.value = value;
    }

    public SelendroidResponse(String sessionId, int status, Exception e) throws JSONException {
        this.value = buildErrorValue(e);
        this.sessionId = sessionId;
        this.status = status;
    }

    public SelendroidResponse(String sessionId, Object value) {
        this.sessionId = sessionId;
        this.status = 0;
        this.value = value;
    }

    public SelendroidResponse(String sessionId, int status, Object value) {
        this.sessionId = sessionId;
        this.status = status;
        this.value = value;
    }

    /*
     * (non-Javadoc)
     *
     * @see io.selendroid.server.Response#getSessionId()
     */
    @Override
    public String getSessionId() {
        return sessionId;
    }

    public int getStatus() {
        return status;
    }

    public Object getValue() {
        return value;
    }

    /*
     * (non-Javadoc)
     *
     * @see io.selendroid.server.Response#render()
     */
    @Override
    public String render() {
        JSONObject o = new JSONObject();
        try {
            if (sessionId != null) {
                o.put("sessionId", sessionId);
            }
            o.put("status", status);
            if (value != null) {
                o.put("value", value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return o.toString();
    }

    private JSONObject buildErrorValue(Throwable t) throws JSONException {
        JSONObject errorValue = new JSONObject();
        errorValue.put("class", t.getClass().getCanonicalName());

        // TODO: Form exception in a way that will be unpacked nicely on the local end.
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        t.printStackTrace(printWriter);
        errorValue.put("message", t.getMessage() + "\n" + stringWriter.toString());

        /*
         * There is no easy way to attach exception 'cause' clauses here.
         * See workaround above which is used instead.
         */
//      JSONArray stackTrace = new JSONArray();
//      for (StackTraceElement el : t.getStackTrace()) {
//          JSONObject frame = new JSONObject();
//          frame.put("lineNumber", el.getLineNumber());
//          frame.put("className", el.getClassName());
//          frame.put("methodName", el.getMethodName());
//          frame.put("fileName", el.getFileName());
//          stackTrace.put(frame);
//      }
//      errorValue.put("stackTrace", stackTrace);

        return errorValue;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy