diff --git a/apix-integrationtests/alfresco/src/main/java/eu/xenit/apix/rest/v1/tests/ConfigurationTest.java b/apix-integrationtests/alfresco/src/main/java/eu/xenit/apix/rest/v1/tests/ConfigurationTest.java index 22d3d0e2..c6f3ef02 100644 --- a/apix-integrationtests/alfresco/src/main/java/eu/xenit/apix/rest/v1/tests/ConfigurationTest.java +++ b/apix-integrationtests/alfresco/src/main/java/eu/xenit/apix/rest/v1/tests/ConfigurationTest.java @@ -1,17 +1,10 @@ package eu.xenit.apix.rest.v1.tests; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import eu.xenit.apix.content.IContentService; import eu.xenit.apix.data.NodeRef; import eu.xenit.apix.data.QName; import eu.xenit.apix.filefolder.IFileFolderService; import eu.xenit.apix.node.INodeService; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.net.URLEncoder; import org.alfresco.model.ContentModel; import org.alfresco.repo.node.archive.NodeArchiveService; import org.alfresco.repo.security.authentication.AuthenticationUtil; @@ -37,12 +30,21 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.MediaType; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.URLEncoder; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.springframework.http.HttpHeaders.CONTENT_TYPE; + /** * Created by kenneth on 14.03.16. */ public class ConfigurationTest extends RestV1BaseTest { - private final static Logger logger = LoggerFactory.getLogger(ConfigurationTest.class); + private static final Logger logger = LoggerFactory.getLogger(ConfigurationTest.class); @Autowired @Qualifier("FileFolderService") @@ -125,20 +127,7 @@ private String makeBasePath() { @Test public void testConfigurationGet() throws IOException, JSONException { - String requestUrl = makeBasePath(); - - HttpResponse response = Request - .Get(requestUrl) - .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) - .execute() - .returnResponse(); - - assertEquals(200, response.getStatusLine().getStatusCode()); - - JSONObject jsonObject = new JSONObject(EntityUtils.toString(response.getEntity())); - - JSONArray jsonFiles = jsonObject.getJSONArray("files"); - + JSONArray jsonFiles = callConfiguration(makeBasePath()); assertEquals(4, jsonFiles.length()); for (int i = 0; i < jsonFiles.length(); i++) { @@ -157,21 +146,20 @@ public void testConfigurationGet() throws IOException, JSONException { } @Test - public void testConfigurationGetFields() throws IOException, JSONException { - String requestUrl = makeBasePath() + "&fields=nodeRef,path,metadata,parsedContent"; - + public void testConfigurationGetJS() throws IOException, JSONException { HttpResponse response = Request - .Get(requestUrl) - .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .Get(makeBasePath()) + .addHeader(HttpHeaders.ACCEPT, "application/js") .execute() .returnResponse(); - assertEquals(200, response.getStatusLine().getStatusCode()); + assertEquals("application/js", response.getFirstHeader(CONTENT_TYPE).getValue()); + } - JSONObject jsonObject = new JSONObject(EntityUtils.toString(response.getEntity())); - - JSONArray jsonFiles = jsonObject.getJSONArray("files"); - + @Test + public void testConfigurationGetFields() throws IOException, JSONException { + String requestUrl = makeBasePath() + "&fields=nodeRef,path,metadata,parsedContent"; + JSONArray jsonFiles = callConfiguration(requestUrl); assertEquals(4, jsonFiles.length()); for (int i = 0; i < jsonFiles.length(); i++) { @@ -207,10 +195,7 @@ public void testConfigurationGetFields() throws IOException, JSONException { } - @Test - public void testConfigurationFilterFields() throws IOException, JSONException { - String requestUrl = makeBasePath() + "&filter.name=" + URLEncoder.encode("\\.yaml$", "UTF-8"); - + private JSONArray callConfiguration(String requestUrl) throws IOException { HttpResponse response = Request .Get(requestUrl) .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) @@ -218,11 +203,19 @@ public void testConfigurationFilterFields() throws IOException, JSONException { .returnResponse(); assertEquals(200, response.getStatusLine().getStatusCode()); + assertEquals("application/json", response.getFirstHeader(CONTENT_TYPE).getValue()); JSONObject jsonObject = new JSONObject(EntityUtils.toString(response.getEntity())); JSONArray jsonFiles = jsonObject.getJSONArray("files"); + return jsonFiles; + } + + @Test + public void testConfigurationFilterFields() throws IOException, JSONException { + String requestUrl = makeBasePath() + "&filter.name=" + URLEncoder.encode("\\.yaml$", "UTF-8"); + JSONArray jsonFiles = callConfiguration(requestUrl); assertEquals(2, jsonFiles.length()); for (int i = 0; i < jsonFiles.length(); i++) { JSONObject jsonFile = jsonFiles.getJSONObject(i); @@ -235,19 +228,7 @@ public void testConfigurationFilterFields() throws IOException, JSONException { @Test public void testConfigurationSubdirectory() throws IOException, JSONException { String requestUrl = makeBasePath() + "/subFolder"; - - HttpResponse response = Request - .Get(requestUrl) - .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) - .execute() - .returnResponse(); - - assertEquals(200, response.getStatusLine().getStatusCode()); - - JSONObject jsonObject = new JSONObject(EntityUtils.toString(response.getEntity())); - - JSONArray jsonFiles = jsonObject.getJSONArray("files"); - + JSONArray jsonFiles = callConfiguration(requestUrl); assertEquals(1, jsonFiles.length()); for (int i = 0; i < jsonFiles.length(); i++) { JSONObject jsonFile = jsonFiles.getJSONObject(i); @@ -267,7 +248,6 @@ public void cleanUp() { removeTestNode(new org.alfresco.service.cmr.repository.NodeRef(testFolder.toString())); } catch (RuntimeException ex) { logger.debug("Did not need to remove mainTestFolder because it did not exist"); - //ex.printStackTrace(); } return null; }; diff --git a/apix-rest-v1/src/main/java/eu/xenit/apix/rest/v1/configuration/ConfigurationWebscript1.java b/apix-rest-v1/src/main/java/eu/xenit/apix/rest/v1/configuration/ConfigurationWebscript1.java index 75f361ff..ca9377de 100644 --- a/apix-rest-v1/src/main/java/eu/xenit/apix/rest/v1/configuration/ConfigurationWebscript1.java +++ b/apix-rest-v1/src/main/java/eu/xenit/apix/rest/v1/configuration/ConfigurationWebscript1.java @@ -6,25 +6,28 @@ import com.gradecak.alfresco.mvc.annotation.AlfrescoAuthentication; import com.gradecak.alfresco.mvc.annotation.AlfrescoTransaction; import com.gradecak.alfresco.mvc.annotation.AuthenticationType; +import com.gradecak.alfresco.mvc.webscript.DispatcherWebscript; import eu.xenit.apix.configuration.ConfigurationFileFlags; import eu.xenit.apix.configuration.ConfigurationService; import eu.xenit.apix.configuration.Configurations; import eu.xenit.apix.rest.v1.ApixV1Webscript; - -import java.io.IOException; -import java.util.Arrays; -import java.util.List; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.lang.Nullable; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + @AlfrescoAuthentication(AuthenticationType.USER) @RestController public class ConfigurationWebscript1 extends ApixV1Webscript { @@ -45,13 +48,14 @@ public ConfigurationWebscript1( } @AlfrescoTransaction(readOnly = true) - @GetMapping(value = "/v1/configuration", consumes = {"application/js"}, produces = {"application/js"}) + @GetMapping(value = "/v1/configuration", produces = {"application/js", MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity getJsConfigurationFiles( @RequestParam(defaultValue = "content,nodeRef", required = false) String[] fields, @RequestParam String searchDirectory, @RequestParam(value = "filter.name", required = false) String nameFilter, - @RequestParam(required = false) String callback + @RequestParam(required = false) String callback, final HttpServletRequest req ) throws IOException { + final WebScriptRequest wsReq = ((DispatcherWebscript.WebscriptRequestWrapper) req).getWebScriptServletRequest(); List fieldsList = Arrays.asList(fields); ConfigurationFileFlags configurationFileFlags = new ConfigurationFileFlags( fieldsList.contains("content"), @@ -61,32 +65,31 @@ public ResponseEntity getJsConfigurationFiles( fieldsList.contains("nodeRef")); Configurations configurations = configurationService .getConfigurationFiles(searchDirectory, nameFilter, configurationFileFlags); + if ("js".equalsIgnoreCase(wsReq.getFormat()) || + "js".equalsIgnoreCase( + getAcceptSubType( + req.getHeader("Accept")))) { + return ResponseEntity.ok() + .contentType(new MediaType("application", "js")) + .body( + String.format("%s(%s)", callback, + mapper.writeValueAsString(configurations))); + } return ResponseEntity.ok() - .body( - String.format("%s(%s)", callback, - mapper.writeValueAsString(configurations))); + .contentType(MediaType.APPLICATION_JSON) + .body(configurations); } - @AlfrescoTransaction(readOnly = true) - @GetMapping(value = "/v1/configuration" , - consumes = {MediaType.APPLICATION_JSON_VALUE}, - produces = {MediaType.APPLICATION_JSON_VALUE}) - public ResponseEntity getConfigurationFiles( - @RequestParam(defaultValue = "content,nodeRef", required = false) String[] fields, - @RequestParam String searchDirectory, - @RequestParam(value = "filter.name", required = false) String nameFilter - ) throws IOException { - List fieldsList = Arrays.asList(fields); - ConfigurationFileFlags configurationFileFlags = new ConfigurationFileFlags( - fieldsList.contains("content"), - fieldsList.contains("path"), - fieldsList.contains("parsedContent"), - fieldsList.contains("metadata"), - fieldsList.contains("nodeRef")); - Configurations configurations = configurationService - .getConfigurationFiles(searchDirectory, nameFilter, configurationFileFlags); - return ResponseEntity.ok() - .body(configurations); + @Nullable + private static String getAcceptSubType(String accept) { + String acceptSubType = null; + if (accept != null) { + String[] acceptSplit = accept.split("/"); + if (acceptSplit.length > 1) { + acceptSubType = acceptSplit[1]; + } + } + return acceptSubType; } @ExceptionHandler(IllegalArgumentException.class)