/* * Copyright 2008 CIPRES project. http://www.phylo.org/ * All Rights Reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for educational, research and non-profit purposes, without * fee, and without a written agreement is hereby granted, provided that the * above copyright notice, this paragraph and the following two paragraphs * appear in all copies. * * Permission to incorporate this software into commercial products may be * obtained by contacting us: * http://www.phylo.org/contactUs.html * * The software program and documentation are supplied "as is". In no event * shall the CIPRES project be liable to any party for direct, indirect, * special, incidental, or consequential damages, including lost profits, * arising out of the use of this software and its documentation, even if * the CIPRES project has been advised of the possibility of such damage. * The CIPRES project specifically disclaims any warranties, including, but * not limited to, the implied warranties of merchantability and fitness for * a particular purpose. The CIPRES project has no obligations to provide * maintenance, support, updates, enhancements, or modifications. */ package org.ngbw.web.actions; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.net.URLEncoder; import org.json.JSONObject; import org.json.JSONArray; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * RegisterUser.java * * @author Lucie Chan * */ public class RegisterUser extends SessionManager { private static final Log logger = LogFactory.getLog(RegisterUser.class); private String fulltext = null; private Map autocompleteList = new HashMap(); @Override public String execute () throws Exception { return INPUT; } public String cancel () throws Exception { return "cancel"; } public String autocompleteList() { buildAutocompleteList(fulltext, true, autocompleteList); if (autocompleteList.size() <= 0) buildAutocompleteList(fulltext, false, autocompleteList); return SUCCESS; } public String buildAutocompleteList(String fulltext, boolean affiliationSearch, Map autocompleteList) { if (fulltext != null) { /* for (String s : autocompleteAllList.keySet()) { if (s.contains(fulltext)) autocompleteList.put(s, s); }*/ try { String urlStr = null; if (affiliationSearch) urlStr = "https://api.ror.org/organizations?affiliation=" + URLEncoder.encode(fulltext, "UTF-8"); else urlStr = "https://api.ror.org/organizations?query.advanced=name:"+ URLEncoder.encode(fulltext, "UTF-8"); URL url = new URL(urlStr); HttpURLConnection http = (HttpURLConnection) url.openConnection (); http.connect (); //int code = http.getResponseCode(); BufferedReader br = null; if (100 <= http.getResponseCode() && http.getResponseCode() <= 399) { br = new BufferedReader(new InputStreamReader(http.getInputStream())); } else { br = new BufferedReader(new InputStreamReader(http.getErrorStream())); } String output; StringBuffer sb = new StringBuffer(); while ((output = br.readLine()) != null) { //logger.info("ror returned value: " + output ); sb.append(output); } if (sb.length() > 0) { JSONObject json = new JSONObject(sb.toString()); JSONArray items = json.getJSONArray("items"); if (items != null) { for (int i = 0; i < items.length(); ++i) { JSONObject json2 = items.getJSONObject(i); String name = null; if (affiliationSearch) { JSONObject organization = json2.getJSONObject("organization"); name = organization.getString("name"); } else name = json2.getString("name"); logger.info("ror organization name = " + name); autocompleteList.put(name, name); } } } } catch (Exception ex) { logger.error("Exception caught while parsing json results"); logger.error(ex); } } return SUCCESS; } public Map getAutocompleteList() { return autocompleteList; } public void setFulltext(String fulltext) { this.fulltext = fulltext; } public String getFulltext() { return fulltext; } }