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;
}
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);
}
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"));
}
Aggregations