Search in sources :

Example 1 with WebServiceException

use of com.openmeap.protocol.WebServiceException 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 WebServiceException

use of com.openmeap.protocol.WebServiceException in project OpenMEAP by OpenMEAP.

the class JsApiCoreImpl method checkForUpdates.

public void checkForUpdates() {
    UpdateHeader updateHeader = null;
    WebServiceException err = null;
    try {
        try {
            updateHeader = updateHandler.checkForUpdate();
            webView.setUpdateHeader(updateHeader, err, activity.getStorage().getBytesFree());
        } catch (WebServiceException e) {
            webView.setUpdateHeader(null, e, activity.getStorage().getBytesFree());
        }
    } catch (LocalStorageException e) {
        ;
    }
}
Also used : WebServiceException(com.openmeap.protocol.WebServiceException) JsUpdateHeader(com.openmeap.protocol.json.JsUpdateHeader) UpdateHeader(com.openmeap.protocol.dto.UpdateHeader) LocalStorageException(com.openmeap.thinclient.LocalStorageException)

Example 3 with WebServiceException

use of com.openmeap.protocol.WebServiceException in project OpenMEAP by OpenMEAP.

the class ApplicationManagementServiceImpl method getApplication.

/*
	 * PRIVATE METHODS
	 */
private Application getApplication(String appName, String appVersionId) throws WebServiceException {
    if (appName == null || appVersionId == null) {
        throw new WebServiceException(WebServiceException.TypeEnum.APPLICATION_VERSION_NOTFOUND, "Both application name and version id must be specified.");
    }
    ModelManager manager = getModelManager();
    // we will need to verify that they have the latest version
    Application application = manager.getModelService().findApplicationByName(appName);
    if (application == null) {
        throw new WebServiceException(WebServiceException.TypeEnum.APPLICATION_NOTFOUND, "The application \"" + appName + "\" was not found.");
    }
    return application;
}
Also used : WebServiceException(com.openmeap.protocol.WebServiceException) ModelManager(com.openmeap.model.ModelManager) Application(com.openmeap.model.dto.Application)

Example 4 with WebServiceException

use of com.openmeap.protocol.WebServiceException in project OpenMEAP by OpenMEAP.

the class ApplicationManagementPortTypeImplTest method testConnectionOpen_verifyInitialVersionIdentifierRecognized.

/**
	 * Verify that no exceptions are thrown and no update returned
	 * when the version SLIC reports is the same as the initial version.
	 */
@Test
public void testConnectionOpen_verifyInitialVersionIdentifierRecognized() throws Exception {
    thrown = false;
    request.getApplication().setVersionId("ApplicationVersion.identifier.bundled");
    com.openmeap.model.dto.Application app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
    Iterator<Deployment> i = new ArrayList<Deployment>(app.getDeployments()).iterator();
    try {
        modelManager.begin();
        while (i.hasNext()) {
            Deployment d = i.next();
            modelManager.delete(d, null);
        }
        modelManager.commit();
    } catch (Exception e) {
        modelManager.rollback();
        throw new Exception(e);
    }
    try {
        response = appMgmtSvc.connectionOpen(request);
    } catch (WebServiceException wse) {
        thrown = true;
    }
    Assert.assertTrue("No update should be returned here", response.getUpdate() == null);
    Assert.assertTrue("The originally bundled application version id should not trigger an exception", thrown == false);
}
Also used : WebServiceException(com.openmeap.protocol.WebServiceException) Deployment(com.openmeap.model.dto.Deployment) Application(com.openmeap.model.dto.Application) WebServiceException(com.openmeap.protocol.WebServiceException) Test(org.junit.Test)

Example 5 with WebServiceException

use of com.openmeap.protocol.WebServiceException in project OpenMEAP by OpenMEAP.

the class AppMgmtClientTest method testRESTOpenConnection.

public void testRESTOpenConnection() throws Exception {
    AppMgmtClientFactory.setDefaultType(RESTAppMgmtClient.class);
    String[] templates = { "xml/connectionResponse-rest-update.json", "xml/connectionResponse-rest-noupdate.json", "xml/connectionResponse.404.text" };
    HttpRequestExecuterFactory.setDefaultType(MockHttpRequestExecuter.class);
    ApplicationManagementService client = AppMgmtClientFactory.newDefault("/nowhere/");
    ConnectionOpenRequest request = new ConnectionOpenRequest();
    request.setApplication(new Application());
    request.getApplication().setInstallation(new ApplicationInstallation());
    request.setSlic(new SLIC());
    // setup the response xml that we'll spoof as though it's from the server
    Hashtable parms = new Hashtable();
    parms.put("AUTH_TOKEN", "auth_token");
    parms.put("UPDATE_TYPE", "required");
    parms.put("UPDATE_URL", "file://none");
    parms.put("STORAGE_NEEDS", String.valueOf(15));
    parms.put("INSTALL_NEEDS", String.valueOf(15));
    parms.put("HASH", "asdf");
    parms.put("HASH_ALG", "MD5");
    parms.put("VERSION_ID", "versionId");
    InputStream inputStream = AppMgmtClientTest.class.getResourceAsStream(templates[0]);
    MockHttpRequestExecuter.setResponseText(Utils.replaceFields(parms, Utils.readInputStream(inputStream, "UTF-8")));
    // setup our request
    SLIC slic = request.getSlic();
    slic.setVersionId("slicVersion");
    Application app = request.getApplication();
    app.setName("appName");
    app.setVersionId("appVersionId");
    ApplicationInstallation appInst = request.getApplication().getInstallation();
    appInst.setUuid("appInstUuid");
    //////////////
    // Verify that a well-formed request will result in a correctly formed response object
    ConnectionOpenResponse response = client.connectionOpen(request);
    Assert.assertTrue(response.getAuthToken().equals("auth_token"));
    Assert.assertTrue(response.getUpdate().getUpdateUrl().equals("file://none"));
    Assert.assertTrue(response.getUpdate().getInstallNeeds().equals(Long.valueOf(16)));
    Assert.assertTrue(response.getUpdate().getStorageNeeds().equals(Long.valueOf(15)));
    Assert.assertTrue(response.getUpdate().getVersionIdentifier().equals("versionId"));
    Assert.assertTrue(response.getUpdate().getHash().getValue().equals("asdf"));
    Assert.assertTrue(response.getUpdate().getHash().getAlgorithm().equals(HashAlgorithm.MD5));
    //////////////
    // Verify that the xml, sans the update header, will generate a response with no update
    MockHttpRequestExecuter.setResponseText(Utils.replaceFields(parms, Utils.readInputStream(AppMgmtClientTest.class.getResourceAsStream(templates[1]), "UTF-8")));
    response = client.connectionOpen(request);
    Assert.assertTrue(response.getAuthToken().equals("auth_token"));
    Assert.assertTrue(response.getUpdate() == null);
    //////////////
    // Verify that a non-200 will result in a WebServiceException
    Boolean thrown = Boolean.FALSE;
    Exception e = null;
    // 304 is not 200
    MockHttpRequestExecuter.setResponseCode(304);
    try {
        response = client.connectionOpen(request);
    } catch (Exception wse) {
        e = wse;
        thrown = Boolean.TRUE;
    }
    Assert.assertTrue(thrown.booleanValue() && e instanceof WebServiceException);
    //////////////
    // Verify that invalid response content will throw an exception
    thrown = Boolean.FALSE;
    e = null;
    MockHttpRequestExecuter.setResponseText(Utils.replaceFields(parms, Utils.readInputStream(AppMgmtClientTest.class.getResourceAsStream(templates[2]), "UTF-8")));
    MockHttpRequestExecuter.setResponseCode(200);
    try {
        response = client.connectionOpen(request);
    } catch (Exception wse) {
        e = wse;
        thrown = Boolean.TRUE;
    }
    Assert.assertTrue(thrown.booleanValue() && e instanceof WebServiceException);
}
Also used : WebServiceException(com.openmeap.protocol.WebServiceException) Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) ConnectionOpenRequest(com.openmeap.protocol.dto.ConnectionOpenRequest) WebServiceException(com.openmeap.protocol.WebServiceException) SLIC(com.openmeap.protocol.dto.SLIC) ApplicationInstallation(com.openmeap.protocol.dto.ApplicationInstallation) ConnectionOpenResponse(com.openmeap.protocol.dto.ConnectionOpenResponse) ApplicationManagementService(com.openmeap.protocol.ApplicationManagementService) Application(com.openmeap.protocol.dto.Application)

Aggregations

WebServiceException (com.openmeap.protocol.WebServiceException)7 Application (com.openmeap.model.dto.Application)3 ConnectionOpenResponse (com.openmeap.protocol.dto.ConnectionOpenResponse)3 Deployment (com.openmeap.model.dto.Deployment)2 ApplicationManagementService (com.openmeap.protocol.ApplicationManagementService)2 ConnectionOpenRequest (com.openmeap.protocol.dto.ConnectionOpenRequest)2 Result (com.openmeap.protocol.dto.Result)2 Hashtable (java.util.Hashtable)2 Test (org.junit.Test)2 HttpRequestException (com.openmeap.http.HttpRequestException)1 HttpResponse (com.openmeap.http.HttpResponse)1 JSONObjectBuilder (com.openmeap.json.JSONObjectBuilder)1 ModelManager (com.openmeap.model.ModelManager)1 Application (com.openmeap.protocol.dto.Application)1 ApplicationInstallation (com.openmeap.protocol.dto.ApplicationInstallation)1 Error (com.openmeap.protocol.dto.Error)1 SLIC (com.openmeap.protocol.dto.SLIC)1 UpdateHeader (com.openmeap.protocol.dto.UpdateHeader)1 JsUpdateHeader (com.openmeap.protocol.json.JsUpdateHeader)1 LocalStorageException (com.openmeap.thinclient.LocalStorageException)1