Search in sources :

Example 1 with WebApplicationException

use of jakarta.ws.rs.WebApplicationException in project OpenGrok by OpenGrok.

the class IndexerUtil method enableProjects.

/**
 * Enable projects in the remote host application.
 * <p>
 * NOTE: performs a check if the projects are already enabled,
 * before making the change request
 *
 * @param host the url to the remote host
 * @throws ResponseProcessingException in case processing of a received HTTP response fails
 * @throws ProcessingException         in case the request processing or subsequent I/O operation fails
 * @throws WebApplicationException     in case the response status code of the response returned by the server is not successful
 */
public static void enableProjects(final String host) throws ResponseProcessingException, ProcessingException, WebApplicationException {
    final Invocation.Builder request = ClientBuilder.newClient().target(host).path("api").path("v1").path("configuration").path("projectsEnabled").request().headers(getWebAppHeaders());
    final String enabled = request.get(String.class);
    if (!Boolean.parseBoolean(enabled)) {
        final Response r = request.put(Entity.text(Boolean.TRUE.toString()));
        if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            throw new WebApplicationException(String.format("Unable to enable projects: %s", r.getStatusInfo().getReasonPhrase()), r.getStatus());
        }
    }
}
Also used : Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) WebApplicationException(jakarta.ws.rs.WebApplicationException)

Example 2 with WebApplicationException

use of jakarta.ws.rs.WebApplicationException in project OpenGrok by OpenGrok.

the class ProjectsController method get.

@GET
@Path("/{project}/property/{field}")
@Produces(MediaType.APPLICATION_JSON)
public Object get(@PathParam("project") String projectName, @PathParam("field") String field) throws IOException {
    // Avoid classification as a taint bug.
    projectName = Laundromat.launderInput(projectName);
    field = Laundromat.launderInput(field);
    Project project = env.getProjects().get(projectName);
    if (project == null) {
        throw new WebApplicationException("cannot find project " + projectName + " to get a property", Response.Status.BAD_REQUEST);
    }
    return ClassUtil.getFieldValue(project, field);
}
Also used : Project(org.opengrok.indexer.configuration.Project) WebApplicationException(jakarta.ws.rs.WebApplicationException) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 3 with WebApplicationException

use of jakarta.ws.rs.WebApplicationException 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 4 with WebApplicationException

use of jakarta.ws.rs.WebApplicationException 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)

Aggregations

WebApplicationException (jakarta.ws.rs.WebApplicationException)4 GET (jakarta.ws.rs.GET)3 Produces (jakarta.ws.rs.Produces)3 Instant (java.time.Instant)2 CorsEnable (org.opengrok.web.api.v1.filter.CorsEnable)2 Path (jakarta.ws.rs.Path)1 Invocation (jakarta.ws.rs.client.Invocation)1 Response (jakarta.ws.rs.core.Response)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 Project (org.opengrok.indexer.configuration.Project)1 SuggesterConfig (org.opengrok.indexer.configuration.SuggesterConfig)1 Suggestions (org.opengrok.suggest.Suggester.Suggestions)1 SuggesterData (org.opengrok.web.api.v1.suggester.model.SuggesterData)1 Authorized (org.opengrok.web.api.v1.suggester.provider.filter.Authorized)1