Search in sources :

Example 1 with JoynrHttpException

use of io.joynr.exceptions.JoynrHttpException in project joynr by bmwcarit.

the class SerializationTest method serializeReplyWithJoynrHttpException.

@Test
public void serializeReplyWithJoynrHttpException() throws IOException {
    JoynrHttpException error = new JoynrHttpException(404, "detail message: JoynrHttpException");
    Reply reply = new Reply(UUID.randomUUID().toString(), error);
    String writeValueAsString = objectMapper.writeValueAsString(reply);
    System.out.println(writeValueAsString);
    Reply receivedReply = objectMapper.readValue(writeValueAsString, Reply.class);
    Assert.assertEquals(reply, receivedReply);
}
Also used : JoynrHttpException(io.joynr.exceptions.JoynrHttpException) Reply(joynr.Reply) Test(org.junit.Test)

Example 2 with JoynrHttpException

use of io.joynr.exceptions.JoynrHttpException in project joynr by bmwcarit.

the class BounceProxyPerformanceReporter method sendPerformanceReportAsHttpRequest.

/**
 * Sends an HTTP request to the monitoring service to report performance
 * measures of a bounce proxy instance.
 *
 * @throws IOException
 */
private void sendPerformanceReportAsHttpRequest() throws IOException {
    final String url = bounceProxyControllerUrl.buildReportPerformanceUrl();
    logger.debug("Using monitoring service URL: {}", url);
    Map<String, Integer> performanceMap = bounceProxyPerformanceMonitor.getAsKeyValuePairs();
    String serializedMessage = objectMapper.writeValueAsString(performanceMap);
    HttpPost postReportPerformance = new HttpPost(url.trim());
    // using http apache constants here because JOYNr constants are in
    // libjoynr which should not be included here
    postReportPerformance.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
    postReportPerformance.setEntity(new StringEntity(serializedMessage, "UTF-8"));
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(postReportPerformance);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode != HttpURLConnection.HTTP_NO_CONTENT) {
            logger.error("Failed to send performance report: {}", response);
            throw new JoynrHttpException(statusCode, "Failed to send performance report.");
        }
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) JoynrHttpException(io.joynr.exceptions.JoynrHttpException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 3 with JoynrHttpException

use of io.joynr.exceptions.JoynrHttpException in project joynr by bmwcarit.

the class BounceProxyShutdownReporter method reportEventAsHttpRequest.

protected void reportEventAsHttpRequest() throws IOException {
    final String url = bounceProxyControllerUrl.buildReportShutdownUrl();
    logger.debug("Using monitoring service URL: {}", url);
    HttpPut putReportShutdown = new HttpPut(url.trim());
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(putReportShutdown);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode != HttpURLConnection.HTTP_NO_CONTENT) {
            // unexpected response
            logger.error("Failed to send shutdown notification: {}", response);
            throw new JoynrHttpException(statusCode, "Failed to send shutdown notification. Bounce Proxy still registered at Monitoring Service.");
        } else {
            logger.debug("Successfully sent shutdown notification");
        }
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) JoynrHttpException(io.joynr.exceptions.JoynrHttpException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpPut(org.apache.http.client.methods.HttpPut)

Example 4 with JoynrHttpException

use of io.joynr.exceptions.JoynrHttpException in project joynr by bmwcarit.

the class BounceProxyStartupReporter method reportEventAsHttpRequest.

/**
 * Reports the lifecycle event to the monitoring service as HTTP request.
 *
 * @throws IOException
 *             if the connection with the bounce proxy controller could not
 *             be established
 * @throws JoynrHttpException
 *             if the bounce proxy responded that registering the bounce
 *             proxy did not succeed
 */
private void reportEventAsHttpRequest() throws IOException {
    final String url = bounceProxyControllerUrl.buildReportStartupUrl(controlledBounceProxyUrl);
    logger.debug("Using monitoring service URL: {}", url);
    HttpPut putReportStartup = new HttpPut(url.trim());
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(putReportStartup);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        switch(statusCode) {
            case HttpURLConnection.HTTP_NO_CONTENT:
                // bounce proxy instance was already known at the bounce proxy
                // controller
                // nothing else to do
                logger.info("Bounce proxy was already registered with the controller");
                break;
            case HttpURLConnection.HTTP_CREATED:
                // bounce proxy instance was registered for the very first time
                // TODO maybe read URL
                logger.info("Registered bounce proxy with controller");
                break;
            default:
                logger.error("Failed to send startup notification: {}", response);
                throw new JoynrHttpException(statusCode, "Failed to send startup notification. Bounce Proxy won't get any channels assigned.");
        }
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) JoynrHttpException(io.joynr.exceptions.JoynrHttpException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpPut(org.apache.http.client.methods.HttpPut)

Aggregations

JoynrHttpException (io.joynr.exceptions.JoynrHttpException)4 StatusLine (org.apache.http.StatusLine)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 HttpPut (org.apache.http.client.methods.HttpPut)2 Reply (joynr.Reply)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 Test (org.junit.Test)1