Search in sources :

Example 6 with JSONObjectBuilder

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

the class ServiceManagementServlet method sendResult.

private void sendResult(HttpServletResponse response, PrintWriter os, Result result) throws IOException {
    try {
        if (result.getStatus() != Result.Status.SUCCESS) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        }
        JSONObject jsonResult = new JSONObjectBuilder().toJSON(result);
        String stringResult = jsonResult.toString(3);
        logger.debug("returning json result: {}", stringResult);
        os.print(jsonResult);
    } catch (JSONException jse) {
        throw new IOException(jse);
    }
    os.flush();
    os.close();
}
Also used : JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) IOException(java.io.IOException)

Example 7 with JSONObjectBuilder

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

the class AdminTest method testUpdateGlobalSettings.

public void testUpdateGlobalSettings() throws Exception {
    // correct location of storage path prefix
    GlobalSettings originalSettings = new GlobalSettings();
    originalSettings.setExternalServiceUrlPrefix(AdminTestHelper.SERVICES_WEB_URL);
    originalSettings.setMaxFileUploadSize(1234550);
    originalSettings.setServiceManagementAuthSalt(AdminTestHelper.SERVICES_WEB_AUTH_SALT);
    originalSettings.setTemporaryStoragePath(AdminTestHelper.ADMIN_WEB_STORAGE);
    // correct cluster node location and path prefix
    ClusterNode node = new ClusterNode();
    node.setServiceWebUrlPrefix(AdminTestHelper.NODE_01_SERVICES_URL);
    node.setFileSystemStoragePathPrefix(AdminTestHelper.NODE_01_STORAGE);
    originalSettings.addClusterNode(node);
    // validate settings stored in database
    String returnBody = Utils.readInputStream(helper.postGlobalSettings(originalSettings).getResponseBody(), FormConstants.CHAR_ENC_DEFAULT);
    logger.info(returnBody);
    modelManager.getModelService().clearPersistenceContext();
    GlobalSettings insertedSettings = modelManager.getGlobalSettings();
    JSONObjectBuilder job = new JSONObjectBuilder();
    String originalSettingsJSON = job.toJSON(originalSettings).toString(3);
    String insertedSettingsJSON = job.toJSON(insertedSettings).toString(3);
    logger.info("original: {}", originalSettingsJSON);
    logger.info("inserted: {}", insertedSettingsJSON);
    Assert.assertEquals(originalSettingsJSON, insertedSettingsJSON);
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) GlobalSettings(com.openmeap.model.dto.GlobalSettings)

Example 8 with JSONObjectBuilder

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

the class ClusterNodeHealthCheckThread method _run.

private void _run() {
    settings = modelManager.getGlobalSettings();
    JSONObjectBuilder builder = new JSONObjectBuilder();
    ClusterNodeRequest request = new ClusterNodeRequest();
    request.setSubject(ClusterNodeRequest.HEALTH_CHECK);
    lastCheckExceptions = new Vector<Exception>();
    while (true) {
        synchronized (this) {
            lastCheckExceptions.clear();
            if (settings.getClusterNodes() != null) {
                for (ClusterNode clusterNode : settings.getClusterNodes()) {
                    try {
                        request.setClusterNode(clusterNode);
                        HttpResponse response = null;
                        try {
                            response = httpRequestExecuter.postContent(clusterNode.getServiceWebUrlPrefix() + "/service-management/?action=" + ClusterNodeRequest.HEALTH_CHECK + "&auth=" + AuthTokenProvider.newAuthToken(settings.getServiceManagementAuthSalt()), builder.toJSON(request).toString(3), FormConstants.CONT_TYPE_JSON);
                        } catch (Exception e) {
                            logger.error(clusterNode.getServiceWebUrlPrefix() + " health check returned exception", e);
                            Throwable t = ExceptionUtils.getRootCause(e);
                            ClusterNode.Status err = null;
                            if (t instanceof ConnectException) {
                                err = ClusterNode.Status.CONNECT_ERROR;
                            } else {
                                err = ClusterNode.Status.ERROR;
                            }
                            synchronized (clusterNode) {
                                clusterNode.setLastStatus(err);
                                clusterNode.setLastStatusMessage(t.getMessage());
                                clusterNode.setLastStatusCheck(new Date());
                            }
                            if (response != null && response.getResponseBody() != null) {
                                Utils.consumeInputStream(response.getResponseBody());
                                response.getResponseBody().close();
                            }
                            continue;
                        }
                        if (response != null && response.getStatusCode() == 200) {
                            String json = Utils.readInputStream(response.getResponseBody(), FormConstants.CHAR_ENC_DEFAULT);
                            JSONObject jsonObj = new JSONObject(json);
                            Result result = (Result) builder.fromJSON(jsonObj, new Result());
                            response.getResponseBody().close();
                            synchronized (clusterNode) {
                                clusterNode.setLastStatus(result.getStatus() == Result.Status.SUCCESS ? ClusterNode.Status.GOOD : ClusterNode.Status.ERROR);
                                clusterNode.setLastStatusMessage(result.getMessage());
                                clusterNode.setLastStatusCheck(new Date());
                            }
                        } else {
                            synchronized (clusterNode) {
                                clusterNode.setLastStatus(ClusterNode.Status.ERROR);
                                String msg = "Service node " + clusterNode.getServiceWebUrlPrefix() + " returned a non-200 status code " + response.getStatusCode() + " " + Utils.readInputStream(response.getResponseBody(), FormConstants.CHAR_ENC_DEFAULT);
                                logger.error(msg);
                                clusterNode.setLastStatusMessage(msg);
                                response.getResponseBody().close();
                                clusterNode.setLastStatusCheck(new Date());
                            }
                        }
                    } catch (Exception e) {
                        logger.error("Exception performing health check", e);
                        lastCheckExceptions.add(e);
                    }
                }
            }
        }
        synchronized (lastCheckExceptions) {
            lastCheckExceptions.notifyAll();
        }
        try {
            Thread.sleep(checkInterval);
        } catch (InterruptedException e) {
            logger.error("Nap interrupted!", e);
        }
    }
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) HttpResponse(com.openmeap.http.HttpResponse) ClusterNodeRequest(com.openmeap.cluster.dto.ClusterNodeRequest) ConnectException(java.net.ConnectException) Date(java.util.Date) Result(com.openmeap.services.dto.Result) JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) ConnectException(java.net.ConnectException)

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