Search in sources :

Example 1 with CorsEnable

use of org.opengrok.web.api.v1.filter.CorsEnable in project OpenGrok by OpenGrok.

the class SearchController method search.

@GET
@CorsEnable
@Produces(MediaType.APPLICATION_JSON)
public SearchResult search(@Context final HttpServletRequest req, @QueryParam(QueryParameters.FULL_SEARCH_PARAM) final String full, // Nearly QueryParameters.DEFS_SEARCH_PARAM
@QueryParam("def") final String def, // Akin to QueryBuilder.REFS_SEARCH_PARAM
@QueryParam("symbol") final String symbol, @QueryParam(QueryParameters.PATH_SEARCH_PARAM) final String path, @QueryParam(QueryParameters.HIST_SEARCH_PARAM) final String hist, @QueryParam(QueryParameters.TYPE_SEARCH_PARAM) final String type, @QueryParam("projects") final List<String> projects, // Akin to QueryParameters.COUNT_PARAM
@QueryParam("maxresults") @DefaultValue(MAX_RESULTS + "") final int maxResults, @QueryParam(QueryParameters.START_PARAM) @DefaultValue(0 + "") final int startDocIndex) {
    try (SearchEngineWrapper engine = new SearchEngineWrapper(full, def, symbol, path, hist, type)) {
        if (!engine.isValid()) {
            throw new WebApplicationException("Invalid request", Response.Status.BAD_REQUEST);
        }
        Instant startTime = Instant.now();
        suggester.onSearch(projects, engine.getQuery());
        Map<String, List<SearchHit>> hits = engine.search(req, projects, startDocIndex, maxResults).stream().collect(Collectors.groupingBy(Hit::getPath, Collectors.mapping(h -> new SearchHit(h.getLine(), h.getLineno()), Collectors.toList())));
        long duration = Duration.between(startTime, Instant.now()).toMillis();
        int endDocument = startDocIndex + hits.size() - 1;
        return new SearchResult(duration, engine.numResults, hits, startDocIndex, endDocument);
    }
}
Also used : WebApplicationException(jakarta.ws.rs.WebApplicationException) Instant(java.time.Instant) ArrayList(java.util.ArrayList) List(java.util.List) CorsEnable(org.opengrok.web.api.v1.filter.CorsEnable) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 2 with CorsEnable

use of org.opengrok.web.api.v1.filter.CorsEnable in project OpenGrok by OpenGrok.

the class SuggesterController method getSuggestions.

/**
 * Returns suggestions based on the search criteria specified in {@code data}.
 * @param data suggester form data
 * @return list of suggestions and other related information
 * @throws ParseException if the Lucene query created from {@code data} could not be parsed
 */
@GET
@Authorized
@CorsEnable
@Produces(MediaType.APPLICATION_JSON)
public Result getSuggestions(@Valid @BeanParam final SuggesterQueryData data) throws ParseException {
    Instant start = Instant.now();
    SuggesterData suggesterData = SuggesterQueryDataParser.parse(data);
    if (suggesterData.getSuggesterQuery() == null) {
        throw new ParseException("Could not determine suggester query");
    }
    SuggesterConfig config = env.getSuggesterConfig();
    modifyDataBasedOnConfiguration(suggesterData, config);
    if (!satisfiesConfiguration(suggesterData, config)) {
        logger.log(Level.FINER, "Suggester request with data {0} does not satisfy configuration settings", data);
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    Suggestions suggestions = suggester.getSuggestions(suggesterData.getProjects(), suggesterData.getSuggesterQuery(), suggesterData.getQuery());
    Instant end = Instant.now();
    long timeInMs = Duration.between(start, end).toMillis();
    return new Result(suggestions.getItems(), suggesterData.getIdentifier(), suggesterData.getSuggesterQueryFieldText(), timeInMs, suggestions.isPartialResult());
}
Also used : Suggestions(org.opengrok.suggest.Suggester.Suggestions) WebApplicationException(jakarta.ws.rs.WebApplicationException) Instant(java.time.Instant) ParseException(org.apache.lucene.queryparser.classic.ParseException) SuggesterData(org.opengrok.web.api.v1.suggester.model.SuggesterData) SuggesterConfig(org.opengrok.indexer.configuration.SuggesterConfig) CorsEnable(org.opengrok.web.api.v1.filter.CorsEnable) Authorized(org.opengrok.web.api.v1.suggester.provider.filter.Authorized) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 3 with CorsEnable

use of org.opengrok.web.api.v1.filter.CorsEnable in project OpenGrok by OpenGrok.

the class HistoryController method get.

@GET
@CorsEnable
@PathAuthorized
@Produces(MediaType.APPLICATION_JSON)
public HistoryDTO get(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("path") final String path, @QueryParam("withFiles") final boolean withFiles, @QueryParam("max") @DefaultValue(MAX_RESULTS + "") final int maxEntries, @QueryParam("start") @DefaultValue(0 + "") final int startIndex) throws HistoryException, IOException, NoPathParameterException {
    File file = toFile(path);
    History history = HistoryGuru.getInstance().getHistory(file, withFiles, true);
    if (history == null) {
        return null;
    }
    return getHistoryDTO(history.getHistoryEntries(maxEntries, startIndex), history.getTags(), startIndex, maxEntries, history.getHistoryEntries().size());
}
Also used : History(org.opengrok.indexer.history.History) FileUtil.toFile(org.opengrok.web.util.FileUtil.toFile) File(java.io.File) CorsEnable(org.opengrok.web.api.v1.filter.CorsEnable) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET) PathAuthorized(org.opengrok.web.api.v1.filter.PathAuthorized)

Example 4 with CorsEnable

use of org.opengrok.web.api.v1.filter.CorsEnable in project OpenGrok by OpenGrok.

the class AnnotationController method getContent.

@GET
@CorsEnable
@PathAuthorized
@Produces(MediaType.APPLICATION_JSON)
public List<AnnotationDTO> getContent(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("path") final String path, @QueryParam("revision") final String revision) throws IOException, NoPathParameterException {
    File file = toFile(path);
    Annotation annotation = HistoryGuru.getInstance().annotate(file, revision == null || revision.isEmpty() ? null : revision);
    ArrayList<AnnotationDTO> annotationList = new ArrayList<>();
    for (int i = 1; i <= annotation.size(); i++) {
        annotationList.add(new AnnotationDTO(annotation.getRevision(i), annotation.getAuthor(i), annotation.getDesc(annotation.getRevision(i)), annotation.getFileVersion(annotation.getRevision(i)) + "/" + annotation.getRevisions().size()));
    }
    return annotationList;
}
Also used : ArrayList(java.util.ArrayList) FileUtil.toFile(org.opengrok.web.util.FileUtil.toFile) File(java.io.File) Annotation(org.opengrok.indexer.history.Annotation) CorsEnable(org.opengrok.web.api.v1.filter.CorsEnable) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET) PathAuthorized(org.opengrok.web.api.v1.filter.PathAuthorized)

Example 5 with CorsEnable

use of org.opengrok.web.api.v1.filter.CorsEnable in project OpenGrok by OpenGrok.

the class FileController method getGenre.

@GET
@CorsEnable
@PathAuthorized
@Path("/genre")
@Produces(MediaType.TEXT_PLAIN)
public String getGenre(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("path") final String path) throws IOException, ParseException, NoPathParameterException {
    File file = toFile(path);
    Document doc;
    if ((doc = getDocument(file)) == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot get document for file");
        return null;
    }
    AbstractAnalyzer.Genre genre = AbstractAnalyzer.Genre.get(doc.get(QueryBuilder.T));
    if (genre == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot get genre from the document");
        return null;
    }
    return genre.toString();
}
Also used : AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer) Document(org.apache.lucene.document.Document) IndexDatabase.getDocument(org.opengrok.indexer.index.IndexDatabase.getDocument) FileUtil.toFile(org.opengrok.web.util.FileUtil.toFile) File(java.io.File) Path(jakarta.ws.rs.Path) CorsEnable(org.opengrok.web.api.v1.filter.CorsEnable) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET) PathAuthorized(org.opengrok.web.api.v1.filter.PathAuthorized)

Aggregations

GET (jakarta.ws.rs.GET)6 Produces (jakarta.ws.rs.Produces)6 CorsEnable (org.opengrok.web.api.v1.filter.CorsEnable)6 File (java.io.File)4 PathAuthorized (org.opengrok.web.api.v1.filter.PathAuthorized)4 FileUtil.toFile (org.opengrok.web.util.FileUtil.toFile)4 Path (jakarta.ws.rs.Path)2 WebApplicationException (jakarta.ws.rs.WebApplicationException)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 Document (org.apache.lucene.document.Document)2 IndexDatabase.getDocument (org.opengrok.indexer.index.IndexDatabase.getDocument)2 List (java.util.List)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 AbstractAnalyzer (org.opengrok.indexer.analysis.AbstractAnalyzer)1 SuggesterConfig (org.opengrok.indexer.configuration.SuggesterConfig)1 Annotation (org.opengrok.indexer.history.Annotation)1 History (org.opengrok.indexer.history.History)1 Suggestions (org.opengrok.suggest.Suggester.Suggestions)1 SuggesterData (org.opengrok.web.api.v1.suggester.model.SuggesterData)1