source: MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/proxy/Pz2Proxy.java @ 1627

Last change on this file since 1627 was 1627, checked in by gaba, 13 years ago

sru - base

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