Search in sources :

Example 1 with ServiceRequestException

use of org.ambraproject.wombat.service.remote.ServiceRequestException in project wombat by PLOS.

the class SearchController method performValidSearch.

private boolean performValidSearch(HttpServletRequest request, Model model, @SiteParam Site site, @RequestParam MultiValueMap<String, String> params, boolean isCsvExport) throws IOException {
    CommonParams commonParams = modelCommonParams(request, model, site, params, isCsvExport);
    String queryString = params.getFirst("q");
    ArticleSearchQuery.Builder query = ArticleSearchQuery.builder().setQuery(queryString).setSimple(commonParams.isSimpleSearch(queryString)).setIsCsvSearch(isCsvExport);
    commonParams.fill(query);
    ArticleSearchQuery queryObj = query.build();
    Map<?, ?> searchResults;
    try {
        searchResults = solrSearchApi.search(queryObj, site);
    } catch (ServiceRequestException sre) {
        model.addAttribute(isInvalidSolrRequest(queryString, sre) ? CANNOT_PARSE_QUERY_ERROR : UNKNOWN_QUERY_ERROR, true);
        // not a valid search - report errors
        return false;
    }
    if (!isCsvExport) {
        searchResults = solrSearchApi.addArticleLinks(searchResults, request, site, siteSet);
        addFiltersToModel(request, model, site, commonParams, queryObj, searchResults);
    }
    model.addAttribute("searchResults", searchResults);
    model.addAttribute("alertQuery", alertService.convertParamsToJson(params));
    // valid search - proceed to return results
    return true;
}
Also used : ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) ServiceRequestException(org.ambraproject.wombat.service.remote.ServiceRequestException)

Example 2 with ServiceRequestException

use of org.ambraproject.wombat.service.remote.ServiceRequestException in project wombat by PLOS.

the class ArticleController method submitMediaCurationRequest.

/**
 * Serves as a POST endpoint to submit media curation requests
 *
 * @param model data passed in from the view
 * @param site  current site
 * @return path to the template
 * @throws IOException
 */
@RequestMapping(name = "submitMediaCurationRequest", value = "/article/submitMediaCurationRequest", method = RequestMethod.POST)
@ResponseBody
public String submitMediaCurationRequest(HttpServletRequest request, Model model, @SiteParam Site site, @RequestParam("doi") String doi, @RequestParam("link") String link, @RequestParam("comment") String comment, @RequestParam("title") String title, @RequestParam("publishedOn") String publishedOn, @RequestParam("name") String name, @RequestParam("email") String email) throws IOException {
    requireNonemptyParameter(doi);
    if (!link.matches("^\\w+://.*")) {
        link = "http://" + link;
    }
    if (!validateMediaCurationInput(model, link, name, email, title, publishedOn)) {
        model.addAttribute("formError", "Invalid values have been submitted.");
        // return model for error reporting
        return jsonService.serialize(model);
    }
    String linkComment = name + ", " + email + "\n" + comment;
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("doi", doi.replaceFirst("info:doi/", "")));
    params.add(new BasicNameValuePair("link", link));
    params.add(new BasicNameValuePair("comment", linkComment));
    params.add(new BasicNameValuePair("title", title));
    params.add(new BasicNameValuePair("publishedOn", publishedOn));
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    String mediaCurationUrl = (String) site.getTheme().getConfigMap("mediaCuration").get("mediaCurationUrl");
    if (mediaCurationUrl == null) {
        throw new RuntimeException("Media curation URL is not configured");
    }
    HttpPost httpPost = new HttpPost(mediaCurationUrl);
    httpPost.setEntity(entity);
    StatusLine statusLine = null;
    try (CloseableHttpResponse response = cachedRemoteReader.getResponse(httpPost)) {
        statusLine = response.getStatusLine();
    } catch (ServiceRequestException e) {
        // This exception is thrown when the submitted link is already present for the article.
        if (e.getStatusCode() == HttpStatus.CONFLICT.value() && e.getResponseBody().equals("The link already exists")) {
            model.addAttribute("formError", "This link has already been submitted. Please submit a different link");
            model.addAttribute("isValid", false);
        } else {
            throw new RuntimeException(e);
        }
    } finally {
        httpPost.releaseConnection();
    }
    if (statusLine != null && statusLine.getStatusCode() != HttpStatus.CREATED.value()) {
        throw new RuntimeException("bad response from media curation server: " + statusLine);
    }
    return jsonService.serialize(model);
}
Also used : StatusLine(org.apache.http.StatusLine) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ServiceRequestException(org.ambraproject.wombat.service.remote.ServiceRequestException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with ServiceRequestException

use of org.ambraproject.wombat.service.remote.ServiceRequestException in project wombat by PLOS.

the class OrcidApiImpl method getOrcidIdFromAuthorizationCode.

/**
 * {@inheritDoc}
 */
@Override
public String getOrcidIdFromAuthorizationCode(Site site, String code) throws IOException, URISyntaxException {
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("client_id", getOrcidClientId(site)));
    params.add(new BasicNameValuePair("client_secret", getOrcidClientSecret(site)));
    params.add(new BasicNameValuePair("grant_type", "authorization_code"));
    params.add(new BasicNameValuePair("code", code));
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    URI orcidAuthUri = getOrcidAuthUri(site);
    HttpPost httpPost = new HttpPost(orcidAuthUri);
    httpPost.setEntity(entity);
    StatusLine statusLine = null;
    Map<String, Object> orcidJson = new HashMap<>();
    log.debug("ORCID API request executing: " + orcidAuthUri);
    try (CloseableHttpResponse response = cachedRemoteReader.getResponse(httpPost)) {
        statusLine = response.getStatusLine();
        String responseJson = EntityUtils.toString(response.getEntity());
        orcidJson = gson.fromJson(responseJson, HashMap.class);
    } catch (ServiceRequestException e) {
        handleOrcidException(e);
    } finally {
        httpPost.releaseConnection();
    }
    if (statusLine != null && statusLine.getStatusCode() != HttpStatus.OK.value()) {
        throw new RuntimeException("bad response from ORCID authorization API: " + statusLine);
    }
    return String.valueOf(orcidJson.get("orcid"));
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ServiceRequestException(org.ambraproject.wombat.service.remote.ServiceRequestException)

Aggregations

ServiceRequestException (org.ambraproject.wombat.service.remote.ServiceRequestException)3 ArrayList (java.util.ArrayList)2 NameValuePair (org.apache.http.NameValuePair)2 StatusLine (org.apache.http.StatusLine)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 HttpPost (org.apache.http.client.methods.HttpPost)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 URI (java.net.URI)1 HashMap (java.util.HashMap)1 ArticleSearchQuery (org.ambraproject.wombat.service.remote.ArticleSearchQuery)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1