Search in sources :

Example 1 with JSONException

use of com.openmeap.thirdparty.org.json.me.JSONException in project OpenMEAP by OpenMEAP.

the class RESTAppMgmtClient method connectionOpen.

public ConnectionOpenResponse connectionOpen(ConnectionOpenRequest request) throws WebServiceException {
    ConnectionOpenResponse response = null;
    Hashtable postData = new Hashtable();
    postData.put(UrlParamConstants.ACTION, "connection-open-request");
    postData.put(UrlParamConstants.DEVICE_UUID, request.getApplication().getInstallation().getUuid());
    postData.put(UrlParamConstants.APP_NAME, request.getApplication().getName());
    postData.put(UrlParamConstants.APP_VERSION, request.getApplication().getVersionId());
    postData.put(UrlParamConstants.APPARCH_HASH, StringUtils.orEmpty(request.getApplication().getHashValue()));
    postData.put(UrlParamConstants.SLIC_VERSION, request.getSlic().getVersionId());
    HttpResponse httpResponse = null;
    InputSource responseInputSource = null;
    String responseText = null;
    try {
        httpResponse = requester.postData(serviceUrl, postData);
        if (httpResponse.getStatusCode() != 200) {
            throw new WebServiceException(WebServiceException.TypeEnum.CLIENT, "Posting to the service resulted in a " + httpResponse.getStatusCode() + " status code");
        }
        responseText = Utils.readInputStream(httpResponse.getResponseBody(), "UTF-8");
    } catch (Exception e) {
        throw new WebServiceException(WebServiceException.TypeEnum.CLIENT, StringUtils.isEmpty(e.getMessage()) ? e.getMessage() : "There's a problem connecting. Check your network or try again later", e);
    }
    // now we parse the response into a ConnectionOpenResponse object
    if (responseText != null) {
        Result result = new Result();
        JSONObjectBuilder builder = new JSONObjectBuilder();
        try {
            result = (Result) builder.fromJSON(new JSONObject(responseText), result);
            if (result.getError() != null) {
                throw new WebServiceException(WebServiceException.TypeEnum.fromValue(result.getError().getCode().value()), result.getError().getMessage());
            }
        } catch (JSONException e) {
            throw new WebServiceException(WebServiceException.TypeEnum.CLIENT, "Unable to parse service response content.");
        }
        response = result.getConnectionOpenResponse();
    }
    return response;
}
Also used : InputSource(org.xml.sax.InputSource) JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) WebServiceException(com.openmeap.protocol.WebServiceException) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Hashtable(java.util.Hashtable) HttpResponse(com.openmeap.http.HttpResponse) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) ConnectionOpenResponse(com.openmeap.protocol.dto.ConnectionOpenResponse) WebServiceException(com.openmeap.protocol.WebServiceException) IOException(java.io.IOException) HttpRequestException(com.openmeap.http.HttpRequestException) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) Result(com.openmeap.protocol.dto.Result)

Example 2 with JSONException

use of com.openmeap.thirdparty.org.json.me.JSONException in project OpenMEAP by OpenMEAP.

the class ApplicationManagementServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Result result = new Result();
    Error err = new Error();
    String action = req.getParameter(UrlParamConstants.ACTION);
    if (action == null)
        action = "";
    if (action.equals("connection-open-request")) {
        result = connectionOpenRequest(req);
    } else if (action.equals("archiveDownload")) {
        result = handleArchiveDownload(req, resp);
        if (result == null) {
            return;
        }
    } else {
        err.setCode(ErrorCode.MISSING_PARAMETER);
        err.setMessage("The \"action\" parameter is not recognized, missing, or empty.");
        result.setError(err);
    }
    try {
        JSONObjectBuilder builder = new JSONObjectBuilder();
        resp.setContentType("text/javascript");
        JSONObject jsonResult = builder.toJSON(result);
        resp.getOutputStream().write(jsonResult.toString().getBytes());
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Error(com.openmeap.protocol.dto.Error) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) Result(com.openmeap.protocol.dto.Result)

Example 3 with JSONException

use of com.openmeap.thirdparty.org.json.me.JSONException in project OpenMEAP by OpenMEAP.

the class JSONObjectBuilder method toJSON.

public JSONObject toJSON(Object obj) throws JSONException {
    if (obj == null) {
        return null;
    }
    if (!HasJSONProperties.class.isAssignableFrom(obj.getClass())) {
        throw new RuntimeException("The rootObject being converted to JSON must implement the HasJSONProperties interface.");
    }
    JSONProperty[] properties = ((HasJSONProperties) obj).getJSONProperties();
    JSONObject jsonObj = new JSONObject();
    // iterate over each JSONProperty annotated method
    for (int jsonPropertyIdx = 0; jsonPropertyIdx < properties.length; jsonPropertyIdx++) {
        JSONProperty property = properties[jsonPropertyIdx];
        // determine the method return type
        Class returnType = property.getReturnType();
        Object value = property.getGetterSetter().getValue(obj);
        if (value == null) {
            continue;
        }
        if (returnType == null) {
            throw new JSONException(obj.getClass().getName() + "." + property.getPropertyName() + " is annotated with JSONProperty, but has no return type." + "  I can't work with this.");
        }
        // strip "get" off the front
        String propertyName = property.getPropertyName();
        try {
            if (Enum.class.isAssignableFrom(returnType)) {
                Enum ret = (Enum) value;
                jsonObj.put(propertyName, ret.value());
            } else if (isSimpleType(returnType)) {
                jsonObj.put(propertyName, handleSimpleType(returnType, property.getGetterSetter().getValue(obj)));
            } else {
                if (returnType.isArray()) {
                    Object[] returnValues = (Object[]) value;
                    JSONArray jsonArray = new JSONArray();
                    for (int returnValueIdx = 0; returnValueIdx < returnValues.length; returnValueIdx++) {
                        Object thisValue = returnValues[returnValueIdx];
                        jsonArray.put(toJSON(thisValue));
                    }
                    jsonObj.put(propertyName, jsonArray);
                } else if (Hashtable.class.isAssignableFrom(returnType)) {
                    Hashtable map = (Hashtable) value;
                    JSONObject jsonMap = new JSONObject();
                    Enumeration enumer = map.keys();
                    while (enumer.hasMoreElements()) {
                        Object key = (String) enumer.nextElement();
                        Object thisValue = (Object) map.get(key);
                        if (isSimpleType(thisValue.getClass())) {
                            jsonMap.put(key.toString(), handleSimpleType(returnType, thisValue));
                        } else {
                            jsonMap.put(key.toString(), toJSON(thisValue));
                        }
                    }
                    jsonObj.put(propertyName, jsonMap);
                } else if (Vector.class.isAssignableFrom(returnType)) {
                    Vector returnValues = (Vector) property.getGetterSetter().getValue(obj);
                    JSONArray jsonArray = new JSONArray();
                    int size = returnValues.size();
                    for (int returnValueIdx = 0; returnValueIdx < size; returnValueIdx++) {
                        Object thisValue = returnValues.elementAt(returnValueIdx);
                        if (isSimpleType(property.getContainedType())) {
                            jsonArray.put(thisValue);
                        } else {
                            jsonArray.put(toJSON(thisValue));
                        }
                    }
                    jsonObj.put(propertyName, jsonArray);
                } else {
                    jsonObj.put(propertyName, toJSON(value));
                }
            }
        } catch (Exception ite) {
            throw new JSONException(ite);
        }
    }
    return jsonObj;
}
Also used : Enum(com.openmeap.json.Enum) Enumeration(java.util.Enumeration) HasJSONProperties(com.openmeap.json.HasJSONProperties) Hashtable(java.util.Hashtable) JSONArray(com.openmeap.thirdparty.org.json.me.JSONArray) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Vector(java.util.Vector)

Example 4 with JSONException

use of com.openmeap.thirdparty.org.json.me.JSONException in project OpenMEAP by OpenMEAP.

the class JsApiCoreImpl method performUpdate.

/**
	 * 
	 * @param header JSON of the update header
	 * @param statusCallBack a status change callback
	 */
public void performUpdate(final String header, final String statusCallBack) {
    // needs to immediately return control to the calling javascript,
    // and pass back download status information via the callback function.
    JsUpdateHeader jsUpdateHeader = null;
    try {
        jsUpdateHeader = new JsUpdateHeader(header);
    } catch (JSONException e) {
        throw new GenericRuntimeException(e);
    }
    UpdateHeader reloadedHeader = jsUpdateHeader.getWrappedObject();
    if (reloadedHeader != null) {
        updateHandler.handleUpdate(reloadedHeader, new UpdateHandler.StatusChangeHandler() {

            public void onStatusChange(UpdateStatus update) {
                try {
                    JSONObject js = new JSONObject("{update:" + header + "}");
                    UpdateException error = update.getError();
                    js.put("bytesDownloaded", update.getBytesDownloaded());
                    js.put("complete", update.getComplete());
                    js.put("error", error != null ? new JsError(error.getUpdateResult().toString(), error.getMessage()).toJSONObject() : null);
                    webView.executeJavascriptFunction(statusCallBack, new String[] { js.toString() });
                } catch (JSONException e) {
                    throw new GenericRuntimeException(e);
                }
            }
        });
    }
}
Also used : UpdateHandler(com.openmeap.thinclient.update.UpdateHandler) UpdateStatus(com.openmeap.thinclient.update.UpdateStatus) JsError(com.openmeap.protocol.json.JsError) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JsUpdateHeader(com.openmeap.protocol.json.JsUpdateHeader) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JsUpdateHeader(com.openmeap.protocol.json.JsUpdateHeader) UpdateHeader(com.openmeap.protocol.dto.UpdateHeader) UpdateException(com.openmeap.thinclient.update.UpdateException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException)

Example 5 with JSONException

use of com.openmeap.thirdparty.org.json.me.JSONException in project OpenMEAP by OpenMEAP.

the class JSONObjectBuilder method fromJSON.

public Object fromJSON(JSONObject jsonObj, Object rootObject) throws JSONException {
    if (jsonObj == null) {
        return null;
    }
    if (!HasJSONProperties.class.isAssignableFrom(rootObject.getClass())) {
        throw new RuntimeException("The rootObject being converted to JSON must implement the HashJSONProperties interface.");
    }
    JSONProperty[] properties = ((HasJSONProperties) rootObject).getJSONProperties();
    for (int jsonPropertyIdx = 0; jsonPropertyIdx < properties.length; jsonPropertyIdx++) {
        JSONProperty property = properties[jsonPropertyIdx];
        Class returnType = property.getReturnType();
        String propertyName = property.getPropertyName();
        try {
            // get the unparsed value from the JSONObject
            Object value = null;
            try {
                value = jsonObj.get(propertyName);
            } catch (JSONException e) {
                continue;
            }
            if (value == JSONObject.NULL) {
                continue;
            }
            if (Enum.class.isAssignableFrom(returnType)) {
                property.getGetterSetter().setValue(rootObject, value);
            } else if (value instanceof JSONArray) {
                JSONArray array = (JSONArray) value;
                Vector list = new Vector();
                for (int i = 0; i < array.length(); i++) {
                    Object obj = array.get(i);
                    if (obj instanceof JSONObject) {
                        Object newObj = (Object) returnType.newInstance();
                        list.addElement(fromJSON((JSONObject) obj, newObj));
                    } else {
                        list.addElement(obj);
                    }
                }
                if (property.getContainedType() != null) {
                    property.getGetterSetter().setValue(rootObject, list);
                } else {
                    property.getGetterSetter().setValue(rootObject, toTypedArray(list));
                }
            } else if (value instanceof JSONObject) {
                Object obj = (Object) returnType.newInstance();
                if (Hashtable.class.isAssignableFrom(returnType)) {
                    Hashtable table = (Hashtable) obj;
                    JSONObject jsonMap = (JSONObject) value;
                    Enumeration keysEnum = jsonMap.keys();
                    while (keysEnum.hasMoreElements()) {
                        String key = (String) keysEnum.nextElement();
                        Object thisValue = jsonMap.get(key);
                        if (thisValue instanceof JSONObject) {
                            Object newObj = (Object) returnType.newInstance();
                            table.put(key, fromJSON((JSONObject) thisValue, newObj));
                        } else {
                            table.put(key, thisValue);
                        }
                    }
                    property.getGetterSetter().setValue(rootObject, table);
                } else {
                    // of the type correct for the
                    property.getGetterSetter().setValue(rootObject, fromJSON((JSONObject) value, obj));
                }
            } else if (isSimpleType(returnType)) {
                property.getGetterSetter().setValue(rootObject, correctCasting(returnType, value));
            }
        } catch (Exception e) {
            throw new JSONException(e);
        }
    }
    return rootObject;
}
Also used : Enumeration(java.util.Enumeration) HasJSONProperties(com.openmeap.json.HasJSONProperties) Hashtable(java.util.Hashtable) JSONArray(com.openmeap.thirdparty.org.json.me.JSONArray) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Vector(java.util.Vector)

Aggregations

JSONException (com.openmeap.thirdparty.org.json.me.JSONException)12 JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)10 IOException (java.io.IOException)6 JSONObjectBuilder (com.openmeap.json.JSONObjectBuilder)4 JSONArray (com.openmeap.thirdparty.org.json.me.JSONArray)4 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)3 Enumeration (java.util.Enumeration)3 Hashtable (java.util.Hashtable)3 HasJSONProperties (com.openmeap.json.HasJSONProperties)2 Result (com.openmeap.protocol.dto.Result)2 Vector (java.util.Vector)2 ClusterNodeRequest (com.openmeap.cluster.dto.ClusterNodeRequest)1 HttpRequestException (com.openmeap.http.HttpRequestException)1 HttpResponse (com.openmeap.http.HttpResponse)1 Enum (com.openmeap.json.Enum)1 WebServiceException (com.openmeap.protocol.WebServiceException)1 ConnectionOpenResponse (com.openmeap.protocol.dto.ConnectionOpenResponse)1 Error (com.openmeap.protocol.dto.Error)1 UpdateHeader (com.openmeap.protocol.dto.UpdateHeader)1 JsError (com.openmeap.protocol.json.JsError)1