Added retrieve user VC service.
Change-Id: I6dc6bc4cef43a502545a37dc810283039649b913
diff --git a/full/src/main/java/de/ids_mannheim/korap/constant/VirtualCorpusType.java b/full/src/main/java/de/ids_mannheim/korap/constant/VirtualCorpusType.java
index 43e26af..e51d36c 100644
--- a/full/src/main/java/de/ids_mannheim/korap/constant/VirtualCorpusType.java
+++ b/full/src/main/java/de/ids_mannheim/korap/constant/VirtualCorpusType.java
@@ -3,4 +3,9 @@
public enum VirtualCorpusType {
PREDEFINED, PROJECT, PRIVATE, PUBLISHED;
+
+ public String displayName () {
+ return name().toLowerCase();
+
+ }
}
diff --git a/full/src/main/java/de/ids_mannheim/korap/dto/VirtualCorpusDto.java b/full/src/main/java/de/ids_mannheim/korap/dto/VirtualCorpusDto.java
index 6f44a4c..f75d5d6 100644
--- a/full/src/main/java/de/ids_mannheim/korap/dto/VirtualCorpusDto.java
+++ b/full/src/main/java/de/ids_mannheim/korap/dto/VirtualCorpusDto.java
@@ -1,7 +1,19 @@
package de.ids_mannheim.korap.dto;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
public class VirtualCorpusDto {
- int id;
+ private int id;
+ private String name;
+ private String type;
+ private String status;
+ private String description;
+ private String access;
+ private String createdBy;
+ private int numberOfDoc;
}
diff --git a/full/src/main/java/de/ids_mannheim/korap/dto/converter/VirtualCorpusConverter.java b/full/src/main/java/de/ids_mannheim/korap/dto/converter/VirtualCorpusConverter.java
new file mode 100644
index 0000000..c4920e8
--- /dev/null
+++ b/full/src/main/java/de/ids_mannheim/korap/dto/converter/VirtualCorpusConverter.java
@@ -0,0 +1,34 @@
+package de.ids_mannheim.korap.dto.converter;
+
+import org.springframework.stereotype.Service;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+import de.ids_mannheim.korap.dto.VirtualCorpusDto;
+import de.ids_mannheim.korap.entity.VirtualCorpus;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.utils.JsonUtils;
+
+@Service
+public class VirtualCorpusConverter {
+
+ public VirtualCorpusDto createVirtualCorpusDto (VirtualCorpus vc,
+ String statistics) throws KustvaktException {
+
+ VirtualCorpusDto dto = new VirtualCorpusDto();
+ dto.setId(vc.getId());
+ dto.setName(vc.getName());
+ dto.setCreatedBy(vc.getCreatedBy());
+ dto.setAccess(vc.getRequiredAccess().name());
+ dto.setStatus(vc.getStatus());
+ dto.setDescription(vc.getDescription());
+ dto.setType(vc.getType().displayName());
+
+ JsonNode node = JsonUtils.readTree(statistics);
+ int numberOfDoc = node.at("/documents").asInt();
+ dto.setNumberOfDoc(numberOfDoc);
+
+ return dto;
+
+ }
+}
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java b/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java
index 6064f9a..4cf831a 100644
--- a/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java
@@ -1,5 +1,9 @@
package de.ids_mannheim.korap.service;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -11,8 +15,12 @@
import de.ids_mannheim.korap.config.FullConfiguration;
import de.ids_mannheim.korap.constant.VirtualCorpusType;
import de.ids_mannheim.korap.dao.VirtualCorpusDao;
+import de.ids_mannheim.korap.dto.VirtualCorpusDto;
+import de.ids_mannheim.korap.dto.converter.VirtualCorpusConverter;
+import de.ids_mannheim.korap.entity.VirtualCorpus;
import de.ids_mannheim.korap.exceptions.KustvaktException;
import de.ids_mannheim.korap.exceptions.StatusCodes;
+import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface;
import de.ids_mannheim.korap.query.serialize.QuerySerializer;
import de.ids_mannheim.korap.user.User;
import de.ids_mannheim.korap.user.User.CorpusAccess;
@@ -29,16 +37,19 @@
@Autowired
private VirtualCorpusDao dao;
-
@Autowired
private SearchKrill krill;
-
@Autowired
private FullConfiguration config;
+ @Autowired
+ private AuthenticationManagerIface authManager;
+ @Autowired
+ private VirtualCorpusConverter converter;
- public void storeVC (VirtualCorpusFromJson vc, User user)
+ public void storeVC (VirtualCorpusFromJson vc, String username)
throws KustvaktException {
+ User user = authManager.getUser(username);
// EM: how about VirtualCorpusType.PUBLISHED?
if (vc.getType().equals(VirtualCorpusType.PREDEFINED)
&& !user.isAdmin()) {
@@ -100,4 +111,19 @@
jlog.debug("License: " + license + ", number of docs: " + numberOfDoc);
return (numberOfDoc > 0) ? true : false;
}
+
+ public List<VirtualCorpusDto> retrieveUserVC (String username)
+ throws KustvaktException {
+
+ Set<VirtualCorpus> vcs = dao.retrieveVCByUser(username);
+ ArrayList<VirtualCorpusDto> dtos = new ArrayList<>(vcs.size());
+
+ for (VirtualCorpus vc : vcs) {
+ String json = vc.getCollectionQuery();
+ String statistics = krill.getStatistics(json);
+ VirtualCorpusDto vcDto = converter.createVirtualCorpusDto(vc, statistics);
+ dtos.add(vcDto);
+ }
+ return dtos;
+ }
}
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java
index 18fc518..81f293b 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java
@@ -1,8 +1,12 @@
package de.ids_mannheim.korap.web.controller;
+import java.util.List;
+
+import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@@ -15,12 +19,11 @@
import com.sun.jersey.spi.container.ResourceFilters;
+import de.ids_mannheim.korap.dto.VirtualCorpusDto;
import de.ids_mannheim.korap.exceptions.KustvaktException;
import de.ids_mannheim.korap.exceptions.StatusCodes;
-import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface;
import de.ids_mannheim.korap.service.VirtualCorpusService;
import de.ids_mannheim.korap.user.TokenContext;
-import de.ids_mannheim.korap.user.User;
import de.ids_mannheim.korap.utils.JsonUtils;
import de.ids_mannheim.korap.utils.ParameterChecker;
import de.ids_mannheim.korap.web.FullResponseHandler;
@@ -32,15 +35,14 @@
@Controller
@Path("vc")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
-@ResourceFilters({ AuthenticationFilter.class, DemoUserFilter.class, PiwikFilter.class })
+@ResourceFilters({ AuthenticationFilter.class, DemoUserFilter.class,
+ PiwikFilter.class })
public class VirtualCorpusController {
private static Logger jlog =
LoggerFactory.getLogger(VirtualCorpusController.class);
@Autowired
- private AuthenticationManagerIface authManager;
- @Autowired
private FullResponseHandler responseHandler;
@Autowired
private VirtualCorpusService service;
@@ -67,8 +69,7 @@
context.getUsername());
}
- User user = authManager.getUser(context.getUsername());
- service.storeVC(vc, user);
+ service.storeVC(vc, context.getUsername());
}
catch (KustvaktException e) {
throw responseHandler.throwit(e);
@@ -76,4 +77,29 @@
return Response.ok().build();
}
+ @GET
+ @Path("user")
+ public Response getUserVC (@Context SecurityContext securityContext,
+ @QueryParam("userId") String userId) throws KustvaktException {
+
+ TokenContext context =
+ (TokenContext) securityContext.getUserPrincipal();
+ if (context.isDemo()) {
+ throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
+ "Operation is not permitted for user: "
+ + context.getUsername(),
+ context.getUsername());
+ }
+
+ List<VirtualCorpusDto> dtos = service.retrieveUserVC(context.getUsername());
+ String result;
+ try {
+ result = JsonUtils.toJSON(dtos);
+ }
+ catch (KustvaktException e) {
+ throw responseHandler.throwit(e);
+ }
+ return Response.ok(result).build();
+ }
+
}
diff --git a/full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql b/full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql
index db77480..bad2b01 100644
--- a/full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql
+++ b/full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql
@@ -44,16 +44,20 @@
-- virtual corpora
INSERT INTO virtual_corpus(name, type, required_access, created_by, description, status, collection_query)
- VALUES ("dory VC", "PRIVATE", "FREE", "dory", "test vc", "experimental","sigle=GOE");
+ VALUES ("dory VC", "PRIVATE", "FREE", "dory", "test vc", "experimental",
+ '{"collection":{"@type":"koral:doc","value":"GOE","match":"match:eq","key":"corpusSigle"}}');
INSERT INTO virtual_corpus(name, type, required_access, created_by, description, status, collection_query)
- VALUES ("group VC", "PROJECT", "PUB", "dory", "test vc", "experimental","sigle=GOE");
+ VALUES ("group VC", "PROJECT", "PUB", "dory", "test vc", "experimental",
+ '{"collection":{"@type":"koral:doc","value":"GOE","match":"match:eq","key":"corpusSigle"}}');
INSERT INTO virtual_corpus(name, type, required_access, created_by, description, status, collection_query)
- VALUES ("system VC", "PREDEFINED", "ALL", "system", "test vc", "experimental","sigle=GOE");
+ VALUES ("system VC", "PREDEFINED", "ALL", "system", "test vc", "experimental",
+ '{"collection":{"@type":"koral:doc","value":"GOE","match":"match:eq","key":"corpusSigle"}}');
INSERT INTO virtual_corpus(name, type, required_access, created_by, description, status, collection_query)
- VALUES ("published VC", "PUBLISHED", "ALL", "marlin", "test vc", "experimental","sigle=GOE");
+ VALUES ("published VC", "PUBLISHED", "ALL", "marlin", "test vc", "experimental",
+ '{"collection":{"@type":"koral:doc","value":"GOE","match":"match:eq","key":"corpusSigle"}}');
-- virtual corpus access
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/service/full/VirtualCorpusServiceTest.java b/full/src/test/java/de/ids_mannheim/korap/web/service/full/VirtualCorpusServiceTest.java
index cb83753..be22f88 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/service/full/VirtualCorpusServiceTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/service/full/VirtualCorpusServiceTest.java
@@ -14,10 +14,12 @@
import org.eclipse.jetty.http.HttpHeaders;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
import com.fasterxml.jackson.databind.JsonNode;
+import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.ClientResponse.Status;
+import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.spi.container.ContainerRequest;
import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
@@ -34,6 +36,23 @@
private HttpAuthorizationHandler handler;
@Test
+ public void testRetrieveUserVC () throws UniformInterfaceException,
+ ClientHandlerException, KustvaktException {
+ ClientResponse response = resource().path("vc").path("user")
+ .queryParam("username", "dory")
+ .header(Attributes.AUTHORIZATION,
+ handler.createBasicAuthorizationHeaderValue("dory",
+ "pass"))
+ .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
+
+ .get(ClientResponse.class);
+ String entity = response.getEntity(String.class);
+ assertEquals(Status.OK.getStatusCode(), response.getStatus());
+// System.out.println(entity);
+ JsonNode node = JsonUtils.readTree(entity);
+ assertEquals(3, node.size());
+ }
+ @Test
public void testStoreVC () throws KustvaktException {
String json =
"{\"name\": \"new vc\",\"type\": \"PRIVATE\",\"createdBy\": "
@@ -41,25 +60,42 @@
ClientResponse response = resource().path("vc").path("store")
.header(Attributes.AUTHORIZATION,
- handler.createBasicAuthorizationHeaderValue("user",
+ handler.createBasicAuthorizationHeaderValue("test class",
"pass"))
.header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").entity(json)
.post(ClientResponse.class);
String entity = response.getEntity(String.class);
- System.out.println(entity);
+ assertEquals(Status.OK.getStatusCode(), response.getStatus());
+
+ response = resource().path("vc").path("user")
+ .queryParam("username", "test class")
+ .header(Attributes.AUTHORIZATION,
+ handler.createBasicAuthorizationHeaderValue("test class",
+ "pass"))
+ .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
+
+ .get(ClientResponse.class);
+ entity = response.getEntity(String.class);
+ assertEquals(Status.OK.getStatusCode(), response.getStatus());
+// System.out.println(entity);
+ JsonNode node = JsonUtils.readTree(entity);
+ assertEquals(2, node.size());
+ assertEquals("new vc", node.get(1).get("name").asText());
}
@Test
- public void testStoreVCWithExpiredToken () throws IOException, KustvaktException {
+ public void testStoreVCWithExpiredToken ()
+ throws IOException, KustvaktException {
String json =
"{\"name\": \"new vc\",\"type\": \"PRIVATE\",\"createdBy\": "
+ "\"test class\",\"collectionQuery\": \"corpusSigle=GOE\"}";
- InputStream is = getClass().getClassLoader().getResourceAsStream("test-user.token");
+ InputStream is = getClass().getClassLoader()
+ .getResourceAsStream("test-user.token");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String authToken = reader.readLine();
-
+
ClientResponse response = resource().path("vc").path("store")
.header(Attributes.AUTHORIZATION,
AuthenticationScheme.API.displayName() + " "
@@ -68,6 +104,8 @@
.post(ClientResponse.class);
String entity = response.getEntity(String.class);
+ assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
+
JsonNode node = JsonUtils.readTree(entity);
assertEquals(StatusCodes.EXPIRED, node.at("/errors/0/0").asInt());
assertEquals("Authentication token is expired",
@@ -83,7 +121,7 @@
ClientResponse response = resource().path("vc").path("store")
.entity(json).post(ClientResponse.class);
- assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatus());
+ assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
Set<Entry<String, List<String>>> headers =
response.getHeaders().entrySet();
@@ -119,6 +157,7 @@
.entity(json).post(ClientResponse.class);
String entity = response.getEntity(String.class);
// System.out.println(entity);
+ assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
JsonNode node = JsonUtils.readTree(entity);
assertEquals(StatusCodes.DESERIALIZATION_FAILED,