All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.logspace.jvm.agent.hq.AgentControllerOrderResponseHandler Maven / Gradle / Ivy

The newest version!
/**
 * Logspace
 * Copyright (c) 2015 Indoqa Software Design und Beratung GmbH. All rights reserved.
 * This program and the accompanying materials are made available under the terms of
 * the Eclipse Public License Version 1.0, which accompanies this distribution and
 * is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package io.logspace.jvm.agent.hq;

import java.io.IOException;

import io.logspace.jvm.agent.shaded.apache.http.HttpEntity;
import io.logspace.jvm.agent.shaded.apache.http.HttpResponse;
import io.logspace.jvm.agent.shaded.apache.http.StatusLine;
import io.logspace.jvm.agent.shaded.apache.http.client.HttpResponseException;
import io.logspace.jvm.agent.shaded.apache.http.client.ResponseHandler;

import io.logspace.jvm.agent.api.json.AgentControllerOrdersJsonDeserializer;
import io.logspace.jvm.agent.api.order.AgentControllerOrder;

public class AgentControllerOrderResponseHandler implements ResponseHandler {

    private static final int HTTP_3XX = 300;
    private static final int HTTP_NOT_MODIFIED = 304;

    private String lastModified;

    public String getLastModified() {
        return this.lastModified;
    }

    @Override
    public AgentControllerOrder handleResponse(HttpResponse response) throws IOException {
        StatusLine statusLine = response.getStatusLine();
        HttpEntity entity = response.getEntity();

        if (statusLine.getStatusCode() == HTTP_NOT_MODIFIED) {
            return null;
        }

        if (statusLine.getStatusCode() >= HTTP_3XX) {
            throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }

        this.lastModified = response.getFirstHeader("Last-Modified").getValue();

        try {
            return AgentControllerOrdersJsonDeserializer.fromJson(entity.getContent());
        } catch (Exception e) {
            throw new AgentControllerOrderDeserializationException(
                "Error while parsing the agent order controller JSON provided by the Logspace headquarters.", e);
        }
    }

    public static class AgentControllerOrderDeserializationException extends RuntimeException {

        private static final long serialVersionUID = 1L;

        public AgentControllerOrderDeserializationException(String message, Throwable cause) {
            super(message, cause);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy