source: MDService2/branches/MDService_simple/src/eu/clarin/cmdi/mdservice/action/Pz2ProxyAction.java @ 1509

Last change on this file since 1509 was 1509, checked in by vronk, 13 years ago

intermediate version reworking RepoProxyAction? - adding a target_proxy

File size: 6.4 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.StringReader;
7import java.io.StringWriter;
8import java.net.MalformedURLException;
9import java.net.URISyntaxException;
10import java.net.URL;
11import java.util.HashMap;
12import java.io.ByteArrayInputStream;
13
14import javax.xml.parsers.DocumentBuilder;
15import javax.xml.parsers.DocumentBuilderFactory;
16import javax.xml.parsers.ParserConfigurationException;
17import javax.xml.transform.OutputKeys;
18import javax.xml.transform.Transformer;
19import javax.xml.transform.TransformerException;
20import javax.xml.transform.TransformerFactory;
21import javax.xml.transform.dom.DOMSource;
22import javax.xml.transform.stream.StreamResult;
23import javax.xml.xpath.XPath;
24import javax.xml.xpath.XPathConstants;
25import javax.xml.xpath.XPathExpression;
26import javax.xml.xpath.XPathExpressionException;
27import javax.xml.xpath.XPathFactory;
28
29import org.w3c.dom.Document;
30import org.w3c.dom.Node;
31import org.w3c.dom.NodeList;
32import org.xml.sax.InputSource;
33import org.xml.sax.SAXException;
34
35import eu.clarin.cmdi.mdservice.model.Query;
36import eu.clarin.cmdi.mdservice.model.WorkspaceProfile;
37
38/**
39 
40 */
41public class Pz2ProxyAction extends RepoProxyAction {
42
43        private static final long serialVersionUID = 1L;
44
45        private String proxy_key = "pazpar2";
46       
47        private String command;
48        private String query;
49        private String sessionID;
50        private String sort;
51        private String block;
52       
53        public void setCommand(String command) {
54                this.command = command;
55        }
56        public String getCommand() {
57                return command;
58        }
59        public void setQuery(String query) {
60                this.query = query;
61        }
62        public String getQuery() {
63                return query;
64        }
65        public void setSessionID(String sessionID) {
66                this.sessionID = sessionID;
67        }
68        public String getSessionID() {
69                return sessionID;
70        }
71
72        public void setSort(String sort) {
73                this.sort = sort;
74        }
75        public String getSort() {
76                return sort;
77        }
78        public void setBlock(String block) {
79                this.block = block;
80        }
81        public String getBlock() {
82                return block;
83        }
84       
85        @Override
86        protected void  initialize(){
87                 super.initialize();
88                 this.setActionkey("pazpar2");                         
89         }
90       
91        //TODO cache after analysis
92        @Override
93        public String getCache() {
94                return Cache.SKIP;
95        }
96       
97        public String createURLParams(){
98                String params = "?";
99                if (!(command == null)){
100                        params = params + "command=" + command;
101                }
102                if (!(sessionID == null)){
103                        params = params + "&session=" + sessionID;
104                }
105                if (!(query == null)){
106                        params = params + "&query=" + query;
107                }
108                if (!(getStartItem() == null)){
109                        params = params + "&start=" + getStartItem();
110                }
111                if (!(getMaximumItems() == null)){
112                        params = params + "&num=" + getMaximumItems();
113                }
114                if (!(sort == null)){
115                        params = params + "&sort=" + sort;
116                        if (!(block == null)){
117                                params = params + ":" + block;
118                        }
119                }
120               
121                return params;
122        }
123        @Override
124        public String getBaseURI() {           
125                String uri = WorkspaceProfile.getRepositoryPath(getRepository());               
126                return uri;
127        }
128       
129        @Override
130        public URL getTargetRequest() throws IOException {
131        URL targetURL = null;           
132                       
133                targetURL =new URL( getBaseURL(), createURLParams() );
134                Admin.notifyUser("Pz2.targetURL:" + targetURL);
135            return targetURL;
136        }
137       
138/**
139 * Overrides the default implementation for the case, when the Service shall act as "collector",
140 * waiting for the final result (implemented in getSourcePz2
141 * The distinction is based on the command parameter: if it is given, the Class(Service) acts as a basic proxy
142 * transparently passing the communication between client and target repository. 
143 */
144        @Override
145        public InputStream getSourceStream() throws IOException, NoStylesheetException {
146                if (getCommand() == null){
147                        try {
148                                return getSourcePz2();
149                        } catch (Exception e) {
150                                // TODO Auto-generated catch block
151                                e.printStackTrace();
152                        }
153                } 
154                       
155                return super.getSourceStream();
156        }
157       
158/**
159 * TODO: move to Utils
160 * @param is
161 * @return
162 */
163        public Document getDocument(InputStream is){
164                Document doc = null;
165                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
166        DocumentBuilder docBuilder;
167                try {
168                        docBuilder = docFactory.newDocumentBuilder();
169                        doc = docBuilder.parse(is);
170                } catch (ParserConfigurationException e) {
171                        // TODO Auto-generated catch block
172                        e.printStackTrace();
173                } catch (SAXException e) {
174                        // TODO Auto-generated catch block
175                        e.printStackTrace();
176                } catch (IOException e) {
177                        // TODO Auto-generated catch block
178                        e.printStackTrace();
179                }       
180                return doc;
181        }
182       
183/**
184 * TODO: move to Utils
185 * @param is
186 * @param expression
187 * @return
188 */
189        public String getDocumentData(InputStream is, String expression){
190                String sessionid = "";
191       
192                XPathFactory factory = XPathFactory.newInstance(); 
193            XPath xpath = factory.newXPath(); 
194            XPathExpression expr;
195                try {
196                        expr = xpath.compile(expression);
197                        sessionid = (String) expr.evaluate(getDocument(is), XPathConstants.STRING);
198                } catch (XPathExpressionException e) {
199                        // TODO Auto-generated catch block
200                        e.printStackTrace();
201                }
202                       
203                return sessionid;
204               
205        }
206
207/**
208 * This function wraps the session-based communication with a Pazpar2-target, waiting until all the responses were returned (or timeout occured)
209 * returning the full result at the end.   
210 * @return returns the collected final result as a stream
211 * @throws Exception
212 */
213        public InputStream getSourcePz2() throws Exception {
214               
215                InputStream is = null;
216                // TODO param settings
217                //init
218                setCommand("init");
219                String sessionID = getDocumentData(this.getSourceStream(), "//init/session");
220                // get result
221                setCommand("search");
222                setSessionID(sessionID);
223                setQuery(this.getSquery());
224                String ok = getDocumentData(this.getSourceStream(),"//search/status");
225               
226                String activeclients = "1";
227                setCommand("show");
228                setSort("relevance");
229                setBlock("1");
230                //TODO timeout  settings
231                long startMillis = System.currentTimeMillis();
232                long timeout = 0;
233                if (this.getWPOption("pazpartimeout") == null){
234                        timeout = 1000;
235                } else {
236                        timeout = Integer.parseInt(this.getWPOption("pazpartimeout"));
237                }
238                while (Integer.parseInt(activeclients) > 0){
239                        //activeclients = getDocumentData(this.getSourceStream(), "//show/activeclients");
240                        is = this.getSourceStream();
241                        activeclients = getDocumentData(is, "//show/activeclients");
242                        Thread.sleep(1000); 
243                        Admin.notifyUser("ActiveClients: " + activeclients, Admin.DEBUG);
244                        if ((System.currentTimeMillis() - startMillis)/1000 > timeout) {
245                                break;
246                        }
247                }
248                is = this.getSourceStream();
249                return is;
250        }
251       
252}
Note: See TracBrowser for help on using the repository browser.