Revision ff5c5752
b/src/gr/ebs/gss/server/ejb/ExternalAPIBean.java | ||
---|---|---|
51 | 51 |
import java.io.FileOutputStream; |
52 | 52 |
import java.io.IOException; |
53 | 53 |
import java.io.InputStream; |
54 |
import java.io.StringWriter; |
|
55 | 54 |
import java.io.UnsupportedEncodingException; |
56 | 55 |
import java.net.MalformedURLException; |
57 | 56 |
import java.util.ArrayList; |
... | ... | |
83 | 82 |
import javax.naming.InitialContext; |
84 | 83 |
import javax.naming.NamingException; |
85 | 84 |
import javax.persistence.PersistenceException; |
86 |
import javax.xml.parsers.DocumentBuilder; |
|
87 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
88 |
import javax.xml.parsers.ParserConfigurationException; |
|
89 |
import javax.xml.transform.OutputKeys; |
|
90 |
import javax.xml.transform.Transformer; |
|
91 |
import javax.xml.transform.TransformerConfigurationException; |
|
92 |
import javax.xml.transform.TransformerException; |
|
93 |
import javax.xml.transform.TransformerFactory; |
|
94 |
import javax.xml.transform.dom.DOMSource; |
|
95 |
import javax.xml.transform.stream.StreamResult; |
|
96 |
|
|
97 |
import org.apache.commons.configuration.Configuration; |
|
98 |
import org.apache.commons.httpclient.HttpClient; |
|
99 |
import org.apache.commons.httpclient.HttpException; |
|
100 |
import org.apache.commons.httpclient.NameValuePair; |
|
101 |
import org.apache.commons.httpclient.methods.GetMethod; |
|
102 |
import org.apache.commons.httpclient.methods.PostMethod; |
|
103 |
import org.apache.commons.httpclient.methods.StringRequestEntity; |
|
85 |
|
|
104 | 86 |
import org.apache.commons.lang.StringUtils; |
105 | 87 |
import org.apache.commons.logging.Log; |
106 | 88 |
import org.apache.commons.logging.LogFactory; |
89 |
import org.apache.solr.client.solrj.SolrQuery; |
|
107 | 90 |
import org.apache.solr.client.solrj.SolrServerException; |
108 | 91 |
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; |
109 | 92 |
import org.apache.solr.client.solrj.request.AbstractUpdateRequest; |
110 | 93 |
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest; |
94 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
95 |
import org.apache.solr.common.SolrDocument; |
|
96 |
import org.apache.solr.common.SolrDocumentList; |
|
111 | 97 |
import org.apache.solr.common.SolrException; |
112 | 98 |
import org.apache.solr.common.SolrInputDocument; |
113 | 99 |
import org.hibernate.exception.ConstraintViolationException; |
114 |
import org.w3c.dom.DOMException; |
|
115 |
import org.w3c.dom.Document; |
|
116 |
import org.w3c.dom.Node; |
|
117 |
import org.w3c.dom.NodeList; |
|
118 |
import org.xml.sax.SAXException; |
|
119 | 100 |
|
120 | 101 |
import com.novell.ldap.LDAPAttribute; |
121 | 102 |
import com.novell.ldap.LDAPAttributeSet; |
... | ... | |
1822 | 1803 |
/** |
1823 | 1804 |
* Performs the actuals search on the solr server and returns the results |
1824 | 1805 |
* |
1825 |
* We have to use the dismax query type (instead of the |
|
1826 |
* standard) because it allows for search time field boosting. This is because we can't use indexing |
|
1827 |
* time field boosting due to the patched rich indexing API that does not allow it |
|
1828 |
* |
|
1829 | 1806 |
* @param userId |
1830 | 1807 |
* @param query |
1831 | 1808 |
* @return a List of FileHeader objects |
1832 | 1809 |
*/ |
1833 | 1810 |
private List<FileHeader> search(Long userId, String query) { |
1811 |
List<FileHeader> result = new ArrayList<FileHeader>(); |
|
1834 | 1812 |
try { |
1835 |
HttpClient httpClient = new HttpClient(); |
|
1836 |
|
|
1837 |
GetMethod method = new GetMethod(getConfiguration().getString("solrSelectUrl")); |
|
1838 |
NameValuePair[] params = {new NameValuePair("qt", "dismax"), |
|
1839 |
new NameValuePair("q", query), |
|
1840 |
new NameValuePair("sort", "score desc"), |
|
1841 |
new NameValuePair("indent", "on")}; |
|
1842 |
method.setQueryString(params); |
|
1843 |
int retryCount = 0; |
|
1844 |
int statusCode = 0; |
|
1845 |
String response = null; |
|
1846 |
do { |
|
1847 |
statusCode = httpClient.executeMethod(method); |
|
1848 |
logger.debug("HTTP status: " + statusCode); |
|
1849 |
response = method.getResponseBodyAsString(); |
|
1850 |
logger.debug(response); |
|
1851 |
retryCount++; |
|
1852 |
if (statusCode != 200 && retryCount < 3) |
|
1853 |
try { |
|
1854 |
Thread.sleep(3000); //Give Solr a little time to be available |
|
1855 |
} catch (InterruptedException e) { |
|
1856 |
} |
|
1857 |
} while (statusCode != 200 && retryCount < 3); |
|
1858 |
if (statusCode != 200) |
|
1859 |
throw new EJBException("Search query return error:\n" + response); |
|
1860 |
|
|
1861 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
|
1862 |
DocumentBuilder db = dbf.newDocumentBuilder(); |
|
1863 |
Document doc = db.parse(method.getResponseBodyAsStream()); |
|
1864 |
method.releaseConnection(); |
|
1865 |
|
|
1866 |
Node root = doc.getElementsByTagName("response").item(0); |
|
1867 |
Node lst = root.getFirstChild().getNextSibling(); |
|
1868 |
Node status = lst.getFirstChild().getNextSibling(); |
|
1869 |
if (status.getAttributes().getNamedItem("name").getNodeValue().equals("status") && |
|
1870 |
status.getTextContent().equals("0")) { |
|
1871 |
List<FileHeader> fileResult = new ArrayList<FileHeader>(); |
|
1872 |
Node result = lst.getNextSibling().getNextSibling(); |
|
1873 |
NodeList docs = result.getChildNodes(); |
|
1874 |
User user = getUser(userId); |
|
1875 |
for (int i=1; i<docs.getLength(); i=i+2) { |
|
1876 |
Node d = docs.item(i); |
|
1877 |
NodeList docData = d.getChildNodes(); |
|
1878 |
for (int j=1; j<docData.getLength(); j=j+2) { |
|
1879 |
Node dd = docData.item(j); |
|
1880 |
if (dd.getAttributes().item(0).getNodeName().equals("name") && |
|
1881 |
dd.getAttributes().item(0).getNodeValue().equals("id")) { |
|
1882 |
Long fileId = Long.valueOf(dd.getTextContent()); |
|
1883 |
try { |
|
1884 |
FileHeader file = dao.getEntityById(FileHeader.class, fileId); |
|
1885 |
if (file.hasReadPermission(user)) { |
|
1886 |
fileResult.add(file); |
|
1887 |
logger.debug("File added " + fileId); |
|
1888 |
} |
|
1889 |
} catch (ObjectNotFoundException e) { |
|
1890 |
logger.warn("Search result not found", e); |
|
1891 |
} |
|
1892 |
} |
|
1893 |
} |
|
1813 |
CommonsHttpSolrServer solr = new CommonsHttpSolrServer(getConfiguration().getString("solr.url")); |
|
1814 |
SolrQuery solrQuery = new SolrQuery(query); |
|
1815 |
QueryResponse response = solr.query(solrQuery); |
|
1816 |
SolrDocumentList results = response.getResults(); |
|
1817 |
User user = getUser(userId); |
|
1818 |
for (SolrDocument d : results) { |
|
1819 |
Long id = Long.valueOf((String) d.getFieldValue("id")); |
|
1820 |
try { |
|
1821 |
FileHeader f = dao.getEntityById(FileHeader.class, id); |
|
1822 |
if (f.hasReadPermission(user)) |
|
1823 |
result.add(f); |
|
1824 |
} catch (ObjectNotFoundException e) { |
|
1825 |
logger.warn("Search result id " + id + " cannot be found", e); |
|
1894 | 1826 |
} |
1895 |
return fileResult; |
|
1896 | 1827 |
} |
1897 |
throw new EJBException(); |
|
1898 |
} catch (HttpException e) { |
|
1899 |
throw new EJBException(e); |
|
1900 |
} catch (IOException e) { |
|
1901 |
throw new EJBException(e); |
|
1902 |
} catch (SAXException e) { |
|
1828 |
} catch (MalformedURLException e) { |
|
1829 |
logger.error(e); |
|
1903 | 1830 |
throw new EJBException(e); |
1904 |
} catch (ParserConfigurationException e) { |
|
1831 |
} catch (SolrServerException e) { |
|
1832 |
logger.error(e); |
|
1905 | 1833 |
throw new EJBException(e); |
1906 | 1834 |
} catch (ObjectNotFoundException e) { |
1835 |
logger.error(e); |
|
1907 | 1836 |
throw new EJBException(e); |
1908 | 1837 |
} |
1838 |
return result; |
|
1909 | 1839 |
} |
1910 | 1840 |
|
1911 | 1841 |
@Override |
... | ... | |
2156 | 2086 |
} |
2157 | 2087 |
} |
2158 | 2088 |
|
2159 |
/** |
|
2160 |
* Sends a optimize message to the solr server |
|
2161 |
* |
|
2162 |
* @param httpClient |
|
2163 |
* @param retryCount If the commit fails, it is retried three times. This parameter is passed in the recursive |
|
2164 |
* calls to stop the recursion |
|
2165 |
* @throws UnsupportedEncodingException |
|
2166 |
* @throws IOException |
|
2167 |
* @throws HttpException |
|
2168 |
*/ |
|
2169 |
private void sendOptimize(HttpClient httpClient, int retryCount) throws UnsupportedEncodingException, IOException, HttpException { |
|
2170 |
PostMethod method = null; |
|
2171 |
try { |
|
2172 |
logger.debug("Optimize retry: " + retryCount); |
|
2173 |
method = new PostMethod(getConfiguration().getString("solrUpdateUrl")); |
|
2174 |
method.setRequestEntity(new StringRequestEntity("<optimize/>", "text/xml", "iso8859-1")); |
|
2175 |
int statusCode = httpClient.executeMethod(method); |
|
2176 |
logger.debug("HTTP status: " + statusCode); |
|
2177 |
String response = method.getResponseBodyAsString(); |
|
2178 |
logger.debug(response); |
|
2179 |
if (statusCode != 200 && retryCount < 2) { |
|
2180 |
try { |
|
2181 |
Thread.sleep(10000); //Give Solr a little time to be available |
|
2182 |
} catch (InterruptedException e) { |
|
2183 |
} |
|
2184 |
sendOptimize(httpClient, retryCount + 1); |
|
2185 |
} |
|
2186 |
} |
|
2187 |
finally { |
|
2188 |
if (method != null) |
|
2189 |
method.releaseConnection(); |
|
2190 |
} |
|
2191 |
} |
|
2192 |
|
|
2193 | 2089 |
@Override |
2194 | 2090 |
public FileHeaderDTO createFile(Long userId, Long folderId, String name, String mimeType, long fileSize, String filePath) |
2195 | 2091 |
throws DuplicateNameException, ObjectNotFoundException, GSSIOException, |
... | ... | |
2655 | 2551 |
|
2656 | 2552 |
} |
2657 | 2553 |
|
2658 |
/** |
|
2659 |
* @param id |
|
2660 |
* @param config |
|
2661 |
* @throws ObjectNotFoundException |
|
2662 |
* @throws SolrServerException |
|
2663 |
* @throws IOException |
|
2664 |
* @throws MalformedURLException |
|
2665 |
*/ |
|
2554 |
@Override |
|
2666 | 2555 |
public void postFileToSolr(Long id) { |
2667 | 2556 |
try { |
2668 | 2557 |
FileHeader file = dao.getEntityById(FileHeader.class, id); |
Also available in: Unified diff