Search in sources :

Example 1 with KustvaktException

use of de.ids_mannheim.korap.exceptions.KustvaktException in project Kustvakt by KorAP.

the class SearchService method runPipes.

/**
 * Pipes are service URLs for modifying KoralQuery. A POST request
 * with Content-Type application/json will be sent for each pipe.
 * Kustvakt expects a KoralQuery in JSON format as the pipe response.
 *
 * @param query the original koral query
 * @param pipeArray the pipe service URLs
 * @param serializer the query serializer
 * @return a modified koral query
 * @throws KustvaktException
 */
private String runPipes(String query, String[] pipeArray) throws KustvaktException {
    if (pipeArray != null) {
        for (int i = 0; i < pipeArray.length; i++) {
            String pipeURL = pipeArray[i];
            try {
                URL url = new URL(pipeURL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                connection.setRequestProperty("Accept", "application/json");
                connection.setDoOutput(true);
                OutputStream os = connection.getOutputStream();
                byte[] input = query.getBytes("utf-8");
                os.write(input, 0, input.length);
                String entity = null;
                if (connection.getResponseCode() == HttpStatus.SC_OK) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
                    StringBuilder response = new StringBuilder();
                    String responseLine = null;
                    while ((responseLine = br.readLine()) != null) {
                        response.append(responseLine.trim());
                    }
                    entity = response.toString();
                }
                if (entity != null && !entity.isEmpty()) {
                    query = entity;
                } else {
                    query = handlePipeError(query, pipeURL, connection.getResponseCode() + " " + connection.getResponseMessage());
                }
            } catch (Exception e) {
                query = handlePipeError(query, pipeURL, e.getMessage());
            }
        }
    }
    return query;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) URL(java.net.URL) KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException)

Example 2 with KustvaktException

use of de.ids_mannheim.korap.exceptions.KustvaktException in project Kustvakt by KorAP.

the class SearchService method search.

@SuppressWarnings("unchecked")
public String search(String engine, String username, HttpHeaders headers, String q, String ql, String v, List<String> cqList, String fields, String pipes, Integer pageIndex, Integer pageInteger, String ctx, Integer pageLength, Boolean cutoff, boolean accessRewriteDisabled, boolean showTokens) throws KustvaktException {
    if (pageInteger != null && pageInteger < 1) {
        throw new KustvaktException(StatusCodes.INVALID_ARGUMENT, "page must start from 1", "page");
    }
    String[] pipeArray = null;
    if (pipes != null && !pipes.isEmpty()) {
        pipeArray = pipes.split(",");
    }
    KustvaktConfiguration.BACKENDS eng = this.config.chooseBackend(engine);
    User user = createUser(username, headers);
    CorpusAccess corpusAccess = user.getCorpusAccess();
    // it is not needed because all metadata are public.
    if (accessRewriteDisabled) {
        corpusAccess = CorpusAccess.ALL;
        user.setCorpusAccess(CorpusAccess.ALL);
    }
    QuerySerializer serializer = new QuerySerializer();
    serializer.setQuery(q, ql, v);
    String cq = combineMultipleCorpusQuery(cqList);
    if (cq != null)
        serializer.setCollection(cq);
    List<String> fieldList = convertFieldsToList(fields);
    handleNonPublicFields(fieldList, accessRewriteDisabled, serializer);
    MetaQueryBuilder meta = createMetaQuery(pageIndex, pageInteger, ctx, pageLength, cutoff, corpusAccess, fieldList, accessRewriteDisabled, showTokens);
    serializer.setMeta(meta.raw());
    // - either query, corpus or meta
    if (serializer.hasErrors()) {
        throw new KustvaktException(serializer.toJSON());
    }
    String query = serializer.toJSON();
    if (accessRewriteDisabled && showTokens) {
        Notifications n = new Notifications();
        n.addWarning(StatusCodes.NOT_ALLOWED, "Tokens cannot be shown without access.");
        JsonNode warning = n.toJsonNode();
        query = addWarning(query, warning);
    }
    query = runPipes(query, pipeArray);
    query = this.rewriteHandler.processQuery(query, user);
    if (DEBUG) {
        jlog.debug("the serialized query " + query);
    }
    String result;
    if (eng.equals(KustvaktConfiguration.BACKENDS.NEO4J)) {
        result = searchNeo4J(query, pageLength, meta, false);
    } else {
        result = searchKrill.search(query);
    }
    // jlog.debug("Query result: " + result);
    return result;
}
Also used : KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) KustvaktConfiguration(de.ids_mannheim.korap.config.KustvaktConfiguration) User(de.ids_mannheim.korap.user.User) CorpusAccess(de.ids_mannheim.korap.user.User.CorpusAccess) QuerySerializer(de.ids_mannheim.korap.query.serialize.QuerySerializer) MetaQueryBuilder(de.ids_mannheim.korap.query.serialize.MetaQueryBuilder) JsonNode(com.fasterxml.jackson.databind.JsonNode) Notifications(de.ids_mannheim.korap.response.Notifications)

Example 3 with KustvaktException

use of de.ids_mannheim.korap.exceptions.KustvaktException in project Kustvakt by KorAP.

the class StatisticService method checkVC.

private void checkVC(String json) throws KustvaktException {
    JsonNode node = JsonUtils.readTree(json);
    node = node.at("/collection");
    if (node.has("ref")) {
        String vcName = node.path("ref").asText();
        if (vcName.contains("/")) {
            String[] names = vcName.split("/");
            if (names.length == 2) {
                vcName = names[1];
            }
        }
        String vcInCaching = config.getVcInCaching();
        if (vcName.equals(vcInCaching)) {
            throw new KustvaktException(de.ids_mannheim.korap.exceptions.StatusCodes.CACHING_VC, "VC is currently busy and unaccessible due to " + "caching process", node.get("ref").asText());
        }
    }
}
Also used : KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 4 with KustvaktException

use of de.ids_mannheim.korap.exceptions.KustvaktException in project Kustvakt by KorAP.

the class SearchController method serializeQuery.

// EM: This web service is DISABLED until there is a need for it.
// ND: In case rewrite is supported, it could be used to check the authorization
// scope without searching etc. In case not, it helps to compare queries in
// different query languages.
// MH: ref query parameter removed!
// @GET
// @Path("{version}/query")
// @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response serializeQuery(@Context Locale locale, @Context SecurityContext securityContext, @QueryParam("q") String q, @QueryParam("ql") String ql, @QueryParam("v") String v, @QueryParam("context") String context, @QueryParam("cutoff") Boolean cutoff, @QueryParam("count") Integer pageLength, @QueryParam("offset") Integer pageIndex, @QueryParam("page") Integer startPage, @QueryParam("access-rewrite-disabled") boolean accessRewriteDisabled, @QueryParam("cq") String cq) {
    TokenContext ctx = (TokenContext) securityContext.getUserPrincipal();
    try {
        scopeService.verifyScope(ctx, OAuth2Scope.SERIALIZE_QUERY);
        String result = searchService.serializeQuery(q, ql, v, cq, pageIndex, startPage, pageLength, context, cutoff, accessRewriteDisabled);
        if (DEBUG) {
            jlog.debug("Query: " + result);
        }
        return Response.ok(result).build();
    } catch (KustvaktException e) {
        throw kustvaktResponseHandler.throwit(e);
    }
}
Also used : TokenContext(de.ids_mannheim.korap.security.context.TokenContext) KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException)

Example 5 with KustvaktException

use of de.ids_mannheim.korap.exceptions.KustvaktException in project Kustvakt by KorAP.

the class SearchController method retrieveMatchInfo.

@GET
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Path("{version}/corpus/{corpusId}/{docId}/{textId}/{matchId}")
public Response retrieveMatchInfo(@Context SecurityContext ctx, @Context HttpHeaders headers, @Context Locale locale, @PathParam("corpusId") String corpusId, @PathParam("docId") String docId, @PathParam("textId") String textId, @PathParam("matchId") String matchId, @QueryParam("foundry") Set<String> foundries, @QueryParam("layer") Set<String> layers, @QueryParam("spans") Boolean spans, @QueryParam("expand") String expansion, // Highlights may also be a list of valid highlight classes
@QueryParam("hls") Boolean highlights) throws KustvaktException {
    Boolean expandToSentence = true;
    if (expansion != null && (expansion.equals("false") || expansion.equals("null"))) {
        expandToSentence = false;
    }
    TokenContext tokenContext = (TokenContext) ctx.getUserPrincipal();
    scopeService.verifyScope(tokenContext, OAuth2Scope.MATCH_INFO);
    spans = spans != null ? spans : false;
    highlights = highlights != null ? highlights : false;
    if (layers == null || layers.isEmpty())
        layers = new HashSet<>();
    try {
        String results = searchService.retrieveMatchInfo(corpusId, docId, textId, matchId, foundries, tokenContext.getUsername(), headers, layers, spans, expandToSentence, highlights);
        return Response.ok(results).build();
    } catch (KustvaktException e) {
        throw kustvaktResponseHandler.throwit(e);
    }
}
Also used : TokenContext(de.ids_mannheim.korap.security.context.TokenContext) KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

KustvaktException (de.ids_mannheim.korap.exceptions.KustvaktException)153 TokenContext (de.ids_mannheim.korap.security.context.TokenContext)54 Path (javax.ws.rs.Path)45 Consumes (javax.ws.rs.Consumes)25 POST (javax.ws.rs.POST)23 UserGroup (de.ids_mannheim.korap.entity.UserGroup)22 User (de.ids_mannheim.korap.user.User)19 OAuth2Client (de.ids_mannheim.korap.oauth2.entity.OAuth2Client)18 ResourceFilters (com.sun.jersey.spi.container.ResourceFilters)17 KorAPUser (de.ids_mannheim.korap.user.KorAPUser)15 NoResultException (javax.persistence.NoResultException)15 UserGroupMember (de.ids_mannheim.korap.entity.UserGroupMember)14 Query (javax.persistence.Query)14 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)14 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)14 Produces (javax.ws.rs.Produces)12 QueryDO (de.ids_mannheim.korap.entity.QueryDO)11 DELETE (javax.ws.rs.DELETE)10 GET (javax.ws.rs.GET)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)8