/**
 * Copyright (C) Posten Norge AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package no.digipost.api.client;

import no.digipost.api.client.representations.DocumentEvents;
import no.digipost.api.client.representations.DocumentStatus;
import no.digipost.api.client.representations.Link;
import no.digipost.api.client.util.JAXBContextUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.joda.time.DateTime;

import javax.xml.bind.JAXB;
import java.io.IOException;
import java.io.InputStream;

import static no.digipost.api.client.util.JAXBContextUtils.*;
import static no.digipost.api.client.util.JAXBContextUtils.unmarshal;

public class DocumentCommunicator extends Communicator {

	public DocumentCommunicator(final ApiService apiService, final EventLogger eventLogger) {
		super(apiService, eventLogger);
	}

	public DocumentEvents getDocumentEvents(final String organisation, final String partId, final DateTime from, final DateTime to, final int offset, final int maxResults) {
		try(CloseableHttpResponse response = apiService.getDocumentEvents(organisation, partId, from, to, offset, maxResults)){;
			checkResponse(response, eventLogger);
			return unmarshal(documentEventsContext, response.getEntity().getContent(), DocumentEvents.class);

		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	public InputStream getContent(String path) {
		try(CloseableHttpResponse response = apiService.getContent(path)){
			checkResponse(response, eventLogger);
			return response.getEntity().getContent();

		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	public DocumentStatus getDocumentStatus(Link linkToDocumentStatus) {
		try(CloseableHttpResponse response = apiService.getDocumentStatus(linkToDocumentStatus)){
			checkResponse(response, eventLogger);
			return unmarshal(documentStatusContext, response.getEntity().getContent(), DocumentStatus.class);

		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	public DocumentStatus getDocumentStatus(long senderId, String uuid) {
		try(CloseableHttpResponse response = apiService.getDocumentStatus(senderId, uuid)) {
			checkResponse(response, eventLogger);
			return unmarshal(documentStatusContext, response.getEntity().getContent(), DocumentStatus.class);

		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}
}

@@ -809,61 +809,8 @@
 nk;%0A
-import no.digipost.api.client.util.JAXBContextUtils;%0A
 impo
@@ -866,16 +866,16 @@
 sponse;%0A
+
 import o
@@ -902,36 +902,8 @@
 e;%0A%0A
-import javax.xml.bind.JAXB;%0A
 impo
@@ -955,16 +955,16 @@
 tream;%0A%0A
+
 import s
@@ -1020,78 +1020,8 @@
 s.*;
-%0Aimport static no.digipost.api.client.util.JAXBContextUtils.unmarshal;
 %0A%0Apu
@@ -1771,33 +1771,38 @@
 ng path) %7B%0A%09%09try
-(
+ %7B%0A%09%09%09
 CloseableHttpRes
@@ -1845,18 +1845,17 @@
 nt(path)
-)%7B
+;
 %0A%09%09%09chec

Remove unnecessary closing of the response