|
| 1 | +package gov.nasa.jpl.edrn.labcas.utils; |
| 2 | + |
| 3 | +import java.net.MalformedURLException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.List; |
| 8 | +import java.util.logging.Logger; |
| 9 | + |
| 10 | +import javax.xml.parsers.DocumentBuilder; |
| 11 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 12 | + |
| 13 | +import org.apache.http.HttpEntity; |
| 14 | +import org.apache.http.HttpResponse; |
| 15 | +import org.apache.http.client.HttpClient; |
| 16 | +import org.apache.http.client.methods.HttpPost; |
| 17 | +import org.apache.http.entity.ByteArrayEntity; |
| 18 | +import org.apache.http.impl.client.DefaultHttpClient; |
| 19 | +import org.apache.http.util.EntityUtils; |
| 20 | +import org.apache.oodt.cas.metadata.Metadata; |
| 21 | +import org.apache.solr.client.solrj.SolrQuery; |
| 22 | +import org.apache.solr.client.solrj.SolrServer; |
| 23 | +import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; |
| 24 | +import org.apache.solr.client.solrj.response.QueryResponse; |
| 25 | +import org.apache.solr.common.SolrDocument; |
| 26 | +import org.apache.solr.common.SolrDocumentList; |
| 27 | +import org.springframework.util.StringUtils; |
| 28 | +import org.w3c.dom.Document; |
| 29 | +import org.w3c.dom.Element; |
| 30 | + |
| 31 | +import gov.nasa.jpl.edrn.labcas.Constants; |
| 32 | +import gov.nasa.jpl.edrn.labcas.XmlUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * Class containing general utilities to query/update the Solr index behind an OODT File Manager. |
| 36 | + * |
| 37 | + * @author luca |
| 38 | + * |
| 39 | + */ |
| 40 | +public class SolrUtils { |
| 41 | + |
| 42 | + private final static Logger LOG = Logger.getLogger(SolrUtils.class.getName()); |
| 43 | + |
| 44 | + // default value for SOLR URL |
| 45 | + private static String SOLR_URL = "http://localhost:8080/solr/oodt-fm"; |
| 46 | + |
| 47 | + // IMPORTANT: must re-use the same SolrServer instance across all requests to prevent memory leaks |
| 48 | + // see https://issues.apache.org/jira/browse/SOLR-861 |
| 49 | + // this method instantiates the shared instance of SolrServer |
| 50 | + private static SolrServer solrServer = null; |
| 51 | + static { |
| 52 | + if (System.getenv(Constants.ENV_SOLR_URL)!=null) { |
| 53 | + SOLR_URL = System.getenv(Constants.ENV_SOLR_URL); |
| 54 | + try { |
| 55 | + solrServer = new CommonsHttpSolrServer( SOLR_URL ); |
| 56 | + } catch(MalformedURLException e) { |
| 57 | + e.printStackTrace(); |
| 58 | + LOG.warning(e.getMessage()); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Utility method to query Solr for all products of a given dataset and version. |
| 65 | + * |
| 66 | + * @param datasetName |
| 67 | + * @param datasetVersion |
| 68 | + * @return |
| 69 | + */ |
| 70 | + public static List<String> queryAllProducts(String datasetName, int datasetVersion) { |
| 71 | + |
| 72 | + List<String> ids = new ArrayList<String>(); |
| 73 | + |
| 74 | + // build Solr query |
| 75 | + SolrQuery request = new SolrQuery(); |
| 76 | + request.setQuery("*:*"); |
| 77 | + request.addFilterQuery("Dataset:"+datasetName,"Version:"+datasetVersion); |
| 78 | + request.setRows(Constants.MAX_SOLR_ROWS); |
| 79 | + |
| 80 | + // execute Solr query |
| 81 | + try { |
| 82 | + |
| 83 | + QueryResponse response = solrServer.query( request ); |
| 84 | + SolrDocumentList docs = response.getResults(); |
| 85 | + Iterator<SolrDocument> iter = docs.iterator(); |
| 86 | + while (iter.hasNext()) { |
| 87 | + SolrDocument doc = iter.next(); |
| 88 | + //LOG.fine(doc.toString()); |
| 89 | + String id = (String) doc.getFieldValue("id"); |
| 90 | + LOG.info("Retrieved Solr document id="+id); |
| 91 | + ids.add(id); |
| 92 | + } |
| 93 | + |
| 94 | + } catch(Exception e) { |
| 95 | + e.printStackTrace(); |
| 96 | + LOG.warning(e.getMessage()); // will return empty ids list |
| 97 | + } |
| 98 | + |
| 99 | + return ids; |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Utility method to query Solr for a specific product belonging to a dataset and version. |
| 105 | + * @param datasetName |
| 106 | + * @param datasetVersion |
| 107 | + * @param productName |
| 108 | + * @return |
| 109 | + */ |
| 110 | + public static String queryProduct(String datasetName, int datasetVersion, String productName) { |
| 111 | + |
| 112 | + String id = null; |
| 113 | + |
| 114 | + // build Solr query |
| 115 | + SolrQuery request = new SolrQuery(); |
| 116 | + request.setQuery("*:*"); |
| 117 | + request.addFilterQuery(Constants.METADATA_KEY_DATASET+":"+datasetName, |
| 118 | + Constants.METADATA_KEY_VERSION+":"+datasetVersion, |
| 119 | + Constants.METADATA_KEY_PRODUCT_NAME+":"+productName); |
| 120 | + request.setRows(1); |
| 121 | + |
| 122 | + // execute Solr query |
| 123 | + try { |
| 124 | + |
| 125 | + QueryResponse response = solrServer.query( request ); |
| 126 | + SolrDocumentList docs = response.getResults(); |
| 127 | + Iterator<SolrDocument> iter = docs.iterator(); |
| 128 | + while (iter.hasNext()) { |
| 129 | + SolrDocument doc = iter.next(); |
| 130 | + //LOG.fine(doc.toString()); |
| 131 | + id = (String) doc.getFieldValue("id"); |
| 132 | + LOG.info("Retrieved Solr document id="+id); |
| 133 | + } |
| 134 | + |
| 135 | + } catch(Exception e) { |
| 136 | + e.printStackTrace(); |
| 137 | + LOG.warning(e.getMessage()); |
| 138 | + } |
| 139 | + |
| 140 | + return id; |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Utility method to build a Solr XML update document |
| 146 | + * for all given records and all metadata fields. |
| 147 | + * @param ids |
| 148 | + * @param datasetMetadata |
| 149 | + * @return |
| 150 | + */ |
| 151 | + public static String buildSolrXmlDocument(HashMap<String, Metadata> updateMetadataMap) throws Exception { |
| 152 | + |
| 153 | + // create Solr/XML update document |
| 154 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 155 | + DocumentBuilder builder = dbf.newDocumentBuilder(); |
| 156 | + Document xmlDocument = builder.newDocument(); |
| 157 | + |
| 158 | + // <add> |
| 159 | + Element addElement = xmlDocument.createElement("add"); |
| 160 | + xmlDocument.appendChild(addElement); |
| 161 | + |
| 162 | + // loop over all records that must be updated |
| 163 | + for (String id : updateMetadataMap.keySet()) { |
| 164 | + |
| 165 | + // <doc> |
| 166 | + Element docElement = xmlDocument.createElement("doc"); |
| 167 | + addElement.appendChild(docElement); |
| 168 | + |
| 169 | + // <field name="id">38b6e7e6-3a9b-4565-9d57-37e8104b4fde</field> |
| 170 | + Element fieldElement = xmlDocument.createElement("field"); |
| 171 | + fieldElement.setAttribute("name", "id"); |
| 172 | + fieldElement.insertBefore(xmlDocument.createTextNode(id), fieldElement.getLastChild()); |
| 173 | + docElement.appendChild(fieldElement); |
| 174 | + |
| 175 | + // to add one or more values: |
| 176 | + // <field name="Institution" update="set">Darthmouth</field> |
| 177 | + // <field name="Institution" update="set">Children Hospital</field> |
| 178 | + // to remove a key: |
| 179 | + // <field name="Institution" update="set" null="true"/> |
| 180 | + Metadata metadata = updateMetadataMap.get(id); |
| 181 | + for (String key : metadata.getAllKeys()) { |
| 182 | + for (String val : metadata.getAllMetadata(key)) { |
| 183 | + LOG.info("\t==> XML: Updating dataset metadata key=["+key+"] value=["+val+"]"); |
| 184 | + |
| 185 | + Element metFieldElement = xmlDocument.createElement("field"); |
| 186 | + metFieldElement.setAttribute("name", key); |
| 187 | + metFieldElement.setAttribute("update", "set"); |
| 188 | + if (StringUtils.hasText(val)) { |
| 189 | + // add this value to that key |
| 190 | + metFieldElement.insertBefore(xmlDocument.createTextNode(val), metFieldElement.getLastChild()); |
| 191 | + } else { |
| 192 | + // remove all values for that key |
| 193 | + metFieldElement.setAttribute("null", "true"); |
| 194 | + } |
| 195 | + docElement.appendChild(metFieldElement); |
| 196 | + |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + } // loop over record ids |
| 201 | + |
| 202 | + String xmlString = XmlUtils.xmlToString(xmlDocument); |
| 203 | + LOG.info(xmlString); |
| 204 | + return xmlString; |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Utility method to POST an XML document to Solr |
| 210 | + * @param solrXmlDocument |
| 211 | + */ |
| 212 | + public static void postSolrXml(String solrXmlDocument) { |
| 213 | + |
| 214 | + //String strURL = "http://edrn-frontend.jpl.nasa.gov:8080/solr/oodt-fm/update?commit=true"; |
| 215 | + String solrUpdateUrl = SOLR_URL + "/update?commit=true"; |
| 216 | + |
| 217 | + HttpClient client = new DefaultHttpClient(); |
| 218 | + HttpPost post = new HttpPost(solrUpdateUrl); |
| 219 | + |
| 220 | + try { |
| 221 | + HttpEntity entity = new ByteArrayEntity(solrXmlDocument.getBytes("UTF-8")); |
| 222 | + post.setEntity(entity); |
| 223 | + post.setHeader("Content-Type", "application/xml"); |
| 224 | + HttpResponse response = client.execute(post); |
| 225 | + String result = EntityUtils.toString(response.getEntity()); |
| 226 | + LOG.info("POST result="+result); |
| 227 | + |
| 228 | + } catch(Exception e) { |
| 229 | + LOG.warning(e.getMessage()); |
| 230 | + |
| 231 | + } finally { |
| 232 | + // must release connection |
| 233 | + post.releaseConnection(); |
| 234 | + } |
| 235 | + |
| 236 | + } |
| 237 | + |
| 238 | +} |
0 commit comments