Search in sources :

Example 1 with JSONObjectBuilder

use of com.openmeap.json.JSONObjectBuilder 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 JSONObjectBuilder

use of com.openmeap.json.JSONObjectBuilder in project OpenMEAP by OpenMEAP.

the class ServletManagementServletTest method testRefreshApplication.

@Test
public void testRefreshApplication() throws Exception {
    MockHttpServletRequest request = new Request();
    MockHttpServletResponse response = new MockHttpServletResponse();
    String randomUuid = UUID.randomUUID().toString();
    GlobalSettings settings = modelManager.getGlobalSettings();
    /////////////////
    // validate that finding the application, modifying it, and then finding it again 
    // will return an object with the same modifications.
    Application app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
    app.setName(randomUuid);
    Assert.assertTrue(modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
    modelManager.refresh(app, null);
    app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
    Assert.assertTrue(!modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
    ServiceManagementServlet servlet = new ServiceManagementServlet();
    servlet.setModelManager(modelManager);
    servlet.setModelServiceRefreshHandler(new ModelServiceRefreshHandler());
    servlet.getModelServiceRefreshHandler().setModelManager(modelManager);
    ////////////////////
    // validate the happy path of providing all the required information
    String authSalt = servlet.getAuthSalt();
    String authToken = AuthTokenProvider.newAuthToken(authSalt);
    request.setParameter(UrlParamConstants.REFRESH_TYPE, "Application");
    request.setParameter(UrlParamConstants.REFRESH_OBJ_PKID, "1");
    request.setParameter(UrlParamConstants.AUTH_TOKEN, authToken);
    request.setParameter(UrlParamConstants.ACTION, ModelEntityEventAction.MODEL_REFRESH.getActionName());
    servlet.service(request, response);
    String contentString = response.getContentAsString();
    JSONObjectBuilder job = new JSONObjectBuilder();
    Result result = (Result) job.fromJSON(new JSONObject(contentString), new Result());
    Assert.assertTrue(result.getStatus().equals(Result.Status.SUCCESS));
    Assert.assertTrue(!modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
    ////////////////////
    // validate that failing to provide auth token fails to refresh cache
    app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
    app.setName(randomUuid);
    response = new MockHttpServletResponse();
    request.removeParameter(UrlParamConstants.AUTH_TOKEN);
    request.setParameter(UrlParamConstants.ACTION, ModelEntityEventAction.MODEL_REFRESH.getActionName());
    request.setParameter(UrlParamConstants.REFRESH_TYPE, "Application");
    request.setParameter(UrlParamConstants.REFRESH_OBJ_PKID, "1");
    servlet.service(request, response);
    contentString = response.getContentAsString();
    result = (Result) job.fromJSON(new JSONObject(contentString), new Result());
    Assert.assertTrue(result.getStatus().equals(Result.Status.FAILURE));
    Assert.assertTrue(modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
}
Also used : JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ModelServiceRefreshHandler(com.openmeap.model.event.handler.ModelServiceRefreshHandler) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GlobalSettings(com.openmeap.model.dto.GlobalSettings) Application(com.openmeap.model.dto.Application) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Result(com.openmeap.services.dto.Result) Test(org.junit.Test)

Example 3 with JSONObjectBuilder

use of com.openmeap.json.JSONObjectBuilder in project OpenMEAP by OpenMEAP.

the class AdminTestHelper method getConnectionOpen.

/*
	 * Connection open 
	 */
public Result getConnectionOpen(ApplicationVersion version, String slicVersion) throws HttpRequestException, IOException, JSONException {
    Hashtable<String, Object> postData = new Hashtable<String, Object>();
    postData.put(UrlParamConstants.ACTION, "connection-open-request");
    postData.put(UrlParamConstants.DEVICE_UUID, UUID.randomUUID());
    postData.put(UrlParamConstants.APP_NAME, version.getApplication().getName());
    postData.put(UrlParamConstants.APP_VERSION, version.getIdentifier());
    postData.put(UrlParamConstants.APPARCH_HASH, version.getArchive().getHash());
    postData.put(UrlParamConstants.SLIC_VERSION, slicVersion);
    HttpResponse response = requestExecuter.postData(APP_MGMT_WEB_URL, postData);
    String responseText = Utils.readInputStream(response.getResponseBody(), FormConstants.CHAR_ENC_DEFAULT);
    JSONObjectBuilder job = new JSONObjectBuilder();
    Result result = (Result) job.fromJSON(new JSONObject(responseText), new Result());
    return result;
}
Also used : JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Hashtable(java.util.Hashtable) HttpResponse(com.openmeap.http.HttpResponse) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) Result(com.openmeap.protocol.dto.Result)

Example 4 with JSONObjectBuilder

use of com.openmeap.json.JSONObjectBuilder 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 5 with JSONObjectBuilder

use of com.openmeap.json.JSONObjectBuilder in project OpenMEAP by OpenMEAP.

the class ServiceManagementServlet method healthCheck.

/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
private Result healthCheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String json = Utils.readInputStream(request.getInputStream(), FormConstants.CHAR_ENC_DEFAULT);
    Result result = null;
    try {
        ClusterNodeRequest nodeRequest = (ClusterNodeRequest) new JSONObjectBuilder().fromJSON(new JSONObject(json), new ClusterNodeRequest());
        Map<String, String> properties = (Map<String, String>) context.getBean("openmeapServicesWebPropertiesMap");
        synchronized (properties) {
            properties.put("clusterNodeUrlPrefix", nodeRequest.getClusterNode().getServiceWebUrlPrefix());
            properties.put("fileSystemStoragePathPrefix", nodeRequest.getClusterNode().getFileSystemStoragePathPrefix());
        }
        result = new Result(Result.Status.SUCCESS);
    } catch (JSONException e) {
        result = new Result();
        result.setStatus(Result.Status.FAILURE);
        String msg = "Failed to parse health status check JSON - " + json;
        logger.error(msg);
        result.setMessage(msg);
    }
    return result;
}
Also used : JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) ClusterNodeRequest(com.openmeap.cluster.dto.ClusterNodeRequest) Map(java.util.Map) Result(com.openmeap.services.dto.Result)

Aggregations

JSONObjectBuilder (com.openmeap.json.JSONObjectBuilder)8 JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)7 JSONException (com.openmeap.thirdparty.org.json.me.JSONException)4 HttpResponse (com.openmeap.http.HttpResponse)3 Result (com.openmeap.protocol.dto.Result)3 Result (com.openmeap.services.dto.Result)3 ClusterNodeRequest (com.openmeap.cluster.dto.ClusterNodeRequest)2 ClusterNode (com.openmeap.model.dto.ClusterNode)2 GlobalSettings (com.openmeap.model.dto.GlobalSettings)2 IOException (java.io.IOException)2 Hashtable (java.util.Hashtable)2 HttpRequestException (com.openmeap.http.HttpRequestException)1 Application (com.openmeap.model.dto.Application)1 ModelServiceRefreshHandler (com.openmeap.model.event.handler.ModelServiceRefreshHandler)1 WebServiceException (com.openmeap.protocol.WebServiceException)1 ConnectionOpenResponse (com.openmeap.protocol.dto.ConnectionOpenResponse)1 Error (com.openmeap.protocol.dto.Error)1 GenericRuntimeException (com.openmeap.util.GenericRuntimeException)1 ConnectException (java.net.ConnectException)1 Date (java.util.Date)1