- private <T> T expectSuccess(Logger logger, OperationResult result, Class<T> clazz, String url) {
- if (!result.wasSuccessful()) {
- if (result.getResultCode() == 404) {
- logger.debug("The resource at " + url + " does not exists");
- throw new NoSuchElementException("The resource at " + url + " does not exists");
- }
- throw buildFatalFailure(logger, "Bad response. Code: " + result.getResultCode() + " cause: " + result.getFailureCause());
- }
- if (clazz.isAssignableFrom(Void.class)) {
- return null;
- }
- return unmarshal(result.getResult(), clazz);
- }
-
- private <T> T unmarshal(String content, Class<T> clazz) {
- try {
- return (T) JAXBContext.newInstance(clazz).createUnmarshaller().unmarshal(new StringReader(content));
- } catch (Exception e) {
- throw buildFatalFailure(logger, "Unable to unmarshal content", e);
- }
- }
-
- private String marshall(Object object) {
- try {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- JAXBContext.newInstance(object.getClass()).createMarshaller().marshal(object, bos);
- return bos.toString();
- } catch (Exception e) {
- throw buildFatalFailure(logger, "Unable to marshal content", e);
- }
- }
-
- /**
- * @return the common mandatory headers for AAI requests
- */
- private Map<String, List<String>> buildCommonHeaders() {
- Map<String, List<String>> headers = new HashMap<>();
- headers.put(Headers.ACCEPT, newArrayList(MediaType.APPLICATION_XML_VALUE));
- headers.put(Headers.FROM_APP_ID, newArrayList(SERVICE_NAME));
- return headers;
- }
-
-
- private RestClient buildClient() {
- return buildRawClient().basicAuthUsername(aaiUsername).basicAuthPassword(aaiPassword).authenticationMode(RestAuthenticationMode.SSL_BASIC);