Example usage for org.apache.shiro SecurityUtils getSubject

List of usage examples for org.apache.shiro SecurityUtils getSubject

Introduction

In this page you can find the example usage for org.apache.shiro SecurityUtils getSubject.

Prototype

public static Subject getSubject() 

Source Link

Document

Returns the currently accessible Subject available to the calling code depending on runtime environment.

Usage

From source file:annis.service.internal.QueryService.java

License:Apache License

@GET
@Path("search/annotate")
@Produces("application/xml")
public SaltProject annotate(@QueryParam("q") String query, @QueryParam("corpora") String rawCorpusNames,
        @DefaultValue("0") @QueryParam("offset") String offsetRaw,
        @DefaultValue("10") @QueryParam("limit") String limitRaw,
        @DefaultValue("5") @QueryParam("left") String leftRaw,
        @DefaultValue("5") @QueryParam("right") String rightRaw,
        @QueryParam("seglayer") String segmentationLayer) throws IOException {
    requiredParameter(query, "q", "AnnisQL query");
    requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");

    Subject user = SecurityUtils.getSubject();
    List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
    for (String c : corpusNames) {
        user.checkPermission("query:annotate:" + c);
    }//from  ww  w  . jav a  2s .  c  o  m

    int offset = Integer.parseInt(offsetRaw);
    int limit = Integer.parseInt(limitRaw);
    int left = Math.min(maxContext, Integer.parseInt(leftRaw));
    int right = Math.min(maxContext, Integer.parseInt(rightRaw));

    QueryData data = queryDataFromParameters(query, rawCorpusNames);
    String logParameters = createAnnotateLogParameters(left, right, offset, limit);

    data.addExtension(new LimitOffsetQueryData(offset, limit));
    data.addExtension(new AnnotateQueryData(left, right, segmentationLayer));
    long start = new Date().getTime();
    SaltProject p = annisDao.annotate(data);
    long end = new Date().getTime();
    logQuery("ANNOTATE", query, splitCorpusNamesFromRaw(rawCorpusNames), end - start, logParameters);
    return p;

}

From source file:annis.service.internal.QueryService.java

License:Apache License

@GET
@Path("search/find")
@Produces("application/xml")
public List<Match> find(@QueryParam("q") String query, @QueryParam("corpora") String rawCorpusNames,
        @DefaultValue("0") @QueryParam("offset") String offsetRaw,
        @DefaultValue("10") @QueryParam("limit") String limitRaw) throws IOException {
    requiredParameter(query, "q", "AnnisQL query");
    requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");

    Subject user = SecurityUtils.getSubject();
    List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
    for (String c : corpusNames) {
        user.checkPermission("query:find:" + c);
    }//  ww w  . ja  va  2s  .c om

    int offset = Integer.parseInt(offsetRaw);
    int limit = Integer.parseInt(limitRaw);

    QueryData data = queryDataFromParameters(query, rawCorpusNames);
    data.setCorpusConfiguration(annisDao.getCorpusConfiguration());
    data.addExtension(new LimitOffsetQueryData(offset, limit));

    long start = new Date().getTime();
    List<Match> matches = annisDao.find(data);
    long end = new Date().getTime();
    logQuery("FIND", query, splitCorpusNamesFromRaw(rawCorpusNames), end - start);

    return matches;
}

From source file:annis.service.internal.QueryService.java

License:Apache License

/**
* Get result as matrix in WEKA (ARFF) format.
*//*from  w w w .j  av a  2  s.c  om*/
@GET
@Path("search/matrix")
@Produces("text/plain")
public String matrix(@QueryParam("q") String query, @QueryParam("corpora") String rawCorpusNames,
        @QueryParam("metakeys") String rawMetaKeys) {
    requiredParameter(query, "q", "AnnisQL query");
    requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");

    Subject user = SecurityUtils.getSubject();
    List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
    for (String c : corpusNames) {
        user.checkPermission("query:matrix:" + c);
    }

    QueryData data = queryDataFromParameters(query, rawCorpusNames);

    MatrixQueryData ext = new MatrixQueryData();
    if (rawMetaKeys != null) {
        ext.setMetaKeys(splitMatrixKeysFromRaw(rawMetaKeys));
    }
    data.addExtension(ext);

    long start = new Date().getTime();
    List<AnnotatedMatch> matches = annisDao.matrix(data);
    long end = new Date().getTime();
    logQuery("MATRIX", query, splitCorpusNamesFromRaw(rawCorpusNames), end - start);

    if (matches.isEmpty()) {
        return "(empty)";
    } else {
        return WekaHelper.exportAsArff(matches);
    }
}

From source file:annis.service.internal.QueryService.java

License:Apache License

/**
* Get a graph as {@link SaltProject} of a set of Salt IDs.
*
* @param saltIDs saltIDs must have at least one saltId, more than one id are
* separated by + or space/*from  w  ww.  j  av  a  2  s.  c  o  m*/
* @param leftRaw left context parameter
* @param rightRaw right context parameter
* @return the graph of this hit.
*/
@POST
@Path("search/subgraph")
@Produces({ "application/xml", "application/xmi+xml", "application/xmi+binary" })
public SaltProject subgraph(final SubgraphQuery query) {
    // some robustness stuff
    if (query == null) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
                .type(MediaType.TEXT_PLAIN).entity("missing required request body").build());
    }

    QueryData data = new QueryData();

    data.addExtension(new AnnotateQueryData(query.getLeft(), query.getRight(), query.getSegmentationLayer()));

    Set<String> corpusNames = new TreeSet<String>();

    for (SaltURIGroup singleMatch : query.getMatches().getGroups().values()) {
        // collect list of used corpora and created pseudo QueryNodes for each URI
        List<QueryNode> pseudoNodes = new ArrayList<QueryNode>(singleMatch.getUris().size());
        for (java.net.URI u : singleMatch.getUris()) {
            pseudoNodes.add(new QueryNode());
            corpusNames.add(CommonHelper.getCorpusPath(u).get(0));
        }

        data.addAlternative(pseudoNodes);
    }

    Subject user = SecurityUtils.getSubject();
    for (String c : corpusNames) {
        user.checkPermission("query:subgraph:" + c);
    }

    List<String> corpusNamesList = new LinkedList<String>(corpusNames);
    List<Long> corpusIDs = annisDao.mapCorpusNamesToIds(corpusNamesList);

    data.setCorpusList(corpusIDs);
    data.addExtension(query.getMatches());
    long start = new Date().getTime();
    SaltProject p = annisDao.graph(data);
    long end = new Date().getTime();
    logQuery("SUBGRAPH", "", corpusNamesList, end - start);

    return p;
}

From source file:annis.service.internal.QueryService.java

License:Apache License

@GET
@Path("graphs/{top}/{doc}")
@Produces({ "application/xml", "application/xmi+xml", "application/xmi+binary" })
public SaltProject graph(@PathParam("top") String toplevelCorpusName, @PathParam("doc") String documentName) {

    Subject user = SecurityUtils.getSubject();
    user.checkPermission("query:subgraph:" + toplevelCorpusName);

    try {/*  w  ww . j a  va  2 s . co m*/
        long start = new Date().getTime();
        SaltProject p = annisDao.retrieveAnnotationGraph(toplevelCorpusName, documentName);
        long end = new Date().getTime();
        logQuery("GRAPH", toplevelCorpusName, documentName, end - start);
        return p;
    } catch (Exception ex) {
        log.error("error when accessing graph " + toplevelCorpusName + "/" + documentName, ex);
        throw new WebApplicationException(ex);
    }
}

From source file:annis.service.internal.QueryService.java

License:Apache License

@GET
@Path("corpora")
@Produces("application/xml")
public List<AnnisCorpus> corpora() {
    List<AnnisCorpus> allCorpora = annisDao.listCorpora();
    List<AnnisCorpus> allowedCorpora = new LinkedList<AnnisCorpus>();

    // filter by which corpora the user is allowed to access
    Subject user = SecurityUtils.getSubject();
    for (AnnisCorpus c : allCorpora) {
        if (user.isPermitted("query:*:" + c.getName())) {
            allowedCorpora.add(c);/*from w  w  w . j a  v  a 2  s . c o m*/
        }
    }

    return allowedCorpora;
}

From source file:annis.service.internal.QueryService.java

License:Apache License

@GET
@Path("corpora/{top}/config")
@Produces("application/xml")
public CorpusConfig corpusconfig(@PathParam("top") String toplevelName) {
    Subject user = SecurityUtils.getSubject();
    user.checkPermission("query:config:" + toplevelName);

    Map<String, String> tmp = annisDao.getCorpusConfiguration(toplevelName);
    CorpusConfig result = new CorpusConfig();
    result.setConfig(tmp);/*from   w ww.j  a v  a 2  s .c o m*/
    return result;
}

From source file:annis.service.internal.QueryService.java

License:Apache License

@GET
@Path("corpora/{top}/annotations")
@Produces("application/xml")
public List<AnnisAttribute> annotations(@PathParam("top") String toplevelCorpus,
        @DefaultValue("false") @QueryParam("fetchvalues") String fetchValues,
        @DefaultValue("false") @QueryParam("onlymostfrequentvalues") String onlyMostFrequentValues) {
    Subject user = SecurityUtils.getSubject();
    user.checkPermission("query:annotations:" + toplevelCorpus);

    List<String> list = new LinkedList<String>();
    list.add(toplevelCorpus);/*from   ww  w  . j  a v  a2  s .c om*/
    List<Long> corpusList = annisDao.mapCorpusNamesToIds(list);

    return annisDao.listAnnotations(corpusList, Boolean.parseBoolean(fetchValues),
            Boolean.parseBoolean(onlyMostFrequentValues));
}

From source file:annis.service.internal.QueryService.java

License:Apache License

@GET
@Path("corpora/{top}/metadata")
@Produces("application/xml")
public List<Annotation> getMetadata(@PathParam("top") String toplevelCorpusName) {
    Subject user = SecurityUtils.getSubject();
    user.checkPermission("query:meta:" + toplevelCorpusName);

    return annisDao.listCorpusAnnotations(toplevelCorpusName);
}

From source file:annis.service.internal.QueryService.java

License:Apache License

@GET
@Path("corpora/{top}/{document}/metadata")
@Produces("application/xml")
public List<Annotation> getMetadata(@PathParam("top") String toplevelCorpusName,
        @PathParam("document") String documentName,
        @QueryParam("exclude") @DefaultValue("false") boolean exclude) {
    Subject user = SecurityUtils.getSubject();
    user.checkPermission("query:meta:" + toplevelCorpusName);

    if (documentName == null) {
        documentName = toplevelCorpusName;
    }/*from www .ja  v a 2 s  .  c o m*/

    return annisDao.listCorpusAnnotations(toplevelCorpusName, documentName, exclude);
}