source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/Pz2ProxyAction.java @ 1488

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

output - method, DOCTYPE, encoding issues (trying to unify for xhtml (but not over all xsls yet); test-suite update

File size: 5.1 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;
36
37/**
38 
39 */
40public class Pz2ProxyAction extends GenericProxyAction {
41
42        private static final long serialVersionUID = 1L;
43
44        private String proxy_key = "pazpar2";
45       
46        private String command;
47        private String query;
48        private String sessionID;
49        private String sort;
50        private String block;
51       
52        public void setCommand(String command) {
53                this.command = command;
54        }
55        public String getCommand() {
56                return command;
57        }
58        public void setQuery(String query) {
59                this.query = query;
60        }
61        public String getQuery() {
62                return query;
63        }
64        public void setSessionID(String sessionID) {
65                this.sessionID = sessionID;
66        }
67        public String getSessionID() {
68                return sessionID;
69        }
70
71        public void setSort(String sort) {
72                this.sort = sort;
73        }
74        public String getSort() {
75                return sort;
76        }
77        public void setBlock(String block) {
78                this.block = block;
79        }
80        public String getBlock() {
81                return block;
82        }
83       
84        @Override
85        protected void  initialize(){
86                 super.initialize();
87                 this.setActionkey("pazpar2");                         
88         }
89       
90        //TODO cache after analysis
91        @Override
92        public String getCache() {
93                return Cache.SKIP;
94        }
95       
96        public String createURLParams(){
97                String params = "?";
98                if (!(command == null)){
99                        params = params + "command=" + command;
100                }
101                if (!(sessionID == null)){
102                        params = params + "&session=" + sessionID;
103                }
104                if (!(query == null)){
105                        params = params + "&query=" + query;
106                }
107                if (!(getStartItem() == null)){
108                        params = params + "&start=" + getStartItem();
109                }
110                if (!(getMaximumItems() == null)){
111                        params = params + "&num=" + getMaximumItems();
112                }
113                if (!(sort == null)){
114                        params = params + "&sort=" + sort;
115                        if (!(block == null)){
116                                params = params + ":" + block;
117                        }
118                }
119               
120                return params;
121        }
122        @Override
123        public String getBaseURI() {           
124                String uri = getRepositoryPath();               
125                return uri;
126        }
127       
128        @Override
129        public URL getTargetRequest() throws IOException {
130        URL targetURL = null;           
131                       
132                targetURL =new URL( getBaseURL(), createURLParams() );
133                Admin.notifyUser("Pz2.targetURL:" + targetURL);
134            return targetURL;
135        }
136       
137
138        @Override
139        public InputStream getSourceStream() throws IOException, NoStylesheetException {
140                if (getCommand() == null){
141                        try {
142                                return getSourcePz2();
143                        } catch (Exception e) {
144                                // TODO Auto-generated catch block
145                                e.printStackTrace();
146                        }
147                } 
148                       
149                return super.getSourceStream();
150        }
151       
152        public Document getDocument(InputStream is){
153                Document doc = null;
154                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
155        DocumentBuilder docBuilder;
156                try {
157                        docBuilder = docFactory.newDocumentBuilder();
158                        doc = docBuilder.parse(is);
159                } catch (ParserConfigurationException e) {
160                        // TODO Auto-generated catch block
161                        e.printStackTrace();
162                } catch (SAXException e) {
163                        // TODO Auto-generated catch block
164                        e.printStackTrace();
165                } catch (IOException e) {
166                        // TODO Auto-generated catch block
167                        e.printStackTrace();
168                }       
169                return doc;
170        }
171        public String getDocumentData(InputStream is, String expression){
172                String sessionid = "";
173       
174                XPathFactory factory = XPathFactory.newInstance(); 
175            XPath xpath = factory.newXPath(); 
176            XPathExpression expr;
177                try {
178                        expr = xpath.compile(expression);
179                        sessionid = (String) expr.evaluate(getDocument(is), XPathConstants.STRING);
180                } catch (XPathExpressionException e) {
181                        // TODO Auto-generated catch block
182                        e.printStackTrace();
183                }
184                       
185                return sessionid;
186               
187        }
188        public InputStream getSourcePz2() throws Exception {
189               
190                InputStream is = null;
191               
192                //init
193                setCommand("init");
194                String sessionID = getDocumentData(this.getSourceStream(), "//init/session");
195                // get result
196                setCommand("search");
197                setSessionID(sessionID);
198                setQuery(this.getSquery());
199                String ok = getDocumentData(this.getSourceStream(),"//search/status");
200               
201                String activeclients = "1";
202                setCommand("show");
203                setSort("relevance");
204                setBlock("1");
205                while (Integer.parseInt(activeclients) > 0){
206                        //activeclients = getDocumentData(this.getSourceStream(), "//show/activeclients");
207                        is = this.getSourceStream();
208                        activeclients = getDocumentData(is, "//show/activeclients");
209                       
210                }
211                is = this.getSourceStream();
212                return is;
213        }
214
215       
216}
Note: See TracBrowser for help on using the repository browser.