From 032d09912748d47705d45365bb0c1e9bbdd2f614 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Tue, 7 Nov 2017 10:40:49 +0000 Subject: [PATCH] RM-5837 Finish off test and add util to extract JSON. Use two folders, as copying a file and a folder always passes even with the old code. --- .../org/alfresco/rest/core/v0/APIUtils.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/APIUtils.java diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/APIUtils.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/APIUtils.java new file mode 100644 index 0000000000..5404b483c8 --- /dev/null +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/APIUtils.java @@ -0,0 +1,73 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.rest.core.v0; + +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Helper methods for use with REST APIs. + * + * @author Tom Page + * @since 2.6 + */ +public class APIUtils +{ + /** Logger for this class. */ + private static final Logger LOGGER = LoggerFactory.getLogger(APIUtils.class); + + /** Private constructor for helper class. */ + private APIUtils() + { + } + + /** + * Extract the body of a HTTP response as a JSON object. + * + * @param httpResponse The HTTP response. + * @return A JSON representation of the object. + */ + public static JSONObject convertHTTPResponseToJSON(HttpResponse httpResponse) + { + String source = null; + try + { + source = IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8"); + } + catch (IOException e) + { + throw new IllegalArgumentException("Could not extract JSON from HTTP response.", e); + } + LOGGER.info("Response body:\n{}", source); + return new JSONObject(source); + } +}