Skip to content

Commit

Permalink
Merge pull request #181 from xenit-eu/ALFREDAPI-523
Browse files Browse the repository at this point in the history
ALFREDAPI-523 fix configuration endpoint
  • Loading branch information
hechmi-dammak-xenit authored Jun 27, 2023
2 parents 2390801 + dcbf2d9 commit cf576ee
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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++) {
Expand All @@ -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++) {
Expand Down Expand Up @@ -207,22 +195,27 @@ 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)
.execute()
.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);
Expand All @@ -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);
Expand All @@ -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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String> fieldsList = Arrays.asList(fields);
ConfigurationFileFlags configurationFileFlags = new ConfigurationFileFlags(
fieldsList.contains("content"),
Expand All @@ -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<String> 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)
Expand Down

0 comments on commit cf576ee

Please sign in to comment.