package eu.clarin.cmdi.mdservice.action; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.util.HashMap; import java.io.ByteArrayInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import eu.clarin.cmdi.mdservice.model.Query; import eu.clarin.cmdi.mdservice.model.WorkspaceProfile; /** * Main Struts 2 controller * responds to requests (collections, model, recordset) * by dispatching the requests to appropriate internal methods and filling the inputStream with the result * * It is possible to switch target-repository dynamically (on every request). I.e. the client explicitly selects one of the repositories. * * @author vronk * */ public class RepoProxyAction extends MDRepoProxyAction { private static final long serialVersionUID = 1L; private String proxy_key = "repository"; @Override public String getProxyKey() { return proxy_key; } //TODO remove - just for tests @Override public String getCache() { switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository()))) { case PAZPAR: return Cache.SKIP; case SRU: return Cache.SKIP; case MD: return super.getCache(); default: return super.getCache(); } } //TODO remove after xsl update @Override public String getFullFormat() { switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository()))) { case PAZPAR: return "pazpar22" + this.getFormat(); case SRU: return "sru2" + this.getFormat(); case MD: return super.getFullFormat(); default: return super.getFullFormat(); } } public URL getTargetRequestSRU() throws IOException, NoStylesheetException{ SRUProxyAction sruaction = new SRUProxyAction(); // TODO MD to SRU params // missing collections, columns... sruaction.setOperation("searchRetrieve"); //TODO what version sruaction.setVersion("1.2"); sruaction.setStartRecord(Integer.parseInt(this.getStartItem())); sruaction.setMaximumRecords(Integer.parseInt(this.getMaximumItems())); sruaction.setRepository(getRepository()); sruaction.setQuery(this.fullQueryString()); return sruaction.getTargetRequest(); } public InputStream getSourcePz2() throws Exception { InputStream is = null; Pz2ProxyAction pzaction = new Pz2ProxyAction(); // TODO param settings //init pzaction.setRepository(getRepository()); pzaction.setCommand("init"); String sessionID = Helpers.getDocumentData(pzaction.getSourceStream(), "//init/session"); // get result pzaction.setCommand("search"); pzaction.setSessionID(sessionID); pzaction.setQuery(this.fullQueryString()); String ok = Helpers.getDocumentData(pzaction.getSourceStream(),"//search/status"); String activeclients = "1"; pzaction.setCommand("show"); pzaction.setSort("relevance"); pzaction.setBlock("1"); //TODO timeout settings long startMillis = System.currentTimeMillis(); long timeout = 0; if (WorkspaceProfile.getOption("pazpartimeout") == null){ timeout = 1000; } else { timeout = Integer.parseInt(WorkspaceProfile.getOption("pazpartimeout")); } while (Integer.parseInt(activeclients) > 0){ //activeclients = getDocumentData(this.getSourceStream(), "//show/activeclients"); is = pzaction.getSourceStream(); activeclients = Helpers.getDocumentData(is, "//show/activeclients"); Thread.sleep(1000); Admin.notifyUser("ActiveClients: " + activeclients, Admin.DEBUG); if ((System.currentTimeMillis() - startMillis)/1000 > timeout) { break; } } is = pzaction.getSourceStream(); return is; } @Override public InputStream getSourceStream() throws IOException, NoStylesheetException { switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository()))) { case PAZPAR: try { return getSourcePz2(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } case SRU: return super.getSourceStream();//getSourceSRU(); case MD: return super.getSourceStream(); default: return super.getSourceStream(); } } @Override public URL getTargetRequest() throws IOException { switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository()))) { case PAZPAR: return base_url; case SRU: try { return getTargetRequestSRU(); } catch (NoStylesheetException e) { // TODO Auto-generated catch block e.printStackTrace(); } case MD: return super.getTargetRequest(); default: return super.getTargetRequest(); } } }