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

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

repoaction created ,
options + repositories moved to workspaceprofile

File size: 5.7 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 GenericProxyAction {
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        @Override
140        public InputStream getSourceStream() throws IOException, NoStylesheetException {
141                if (getCommand() == null){
142                        try {
143                                return getSourcePz2();
144                        } catch (Exception e) {
145                                // TODO Auto-generated catch block
146                                e.printStackTrace();
147                        }
148                }
149                       
150                return super.getSourceStream();
151        }
152       
153        public Document getDocument(InputStream is){
154                Document doc = null;
155                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
156        DocumentBuilder docBuilder;
157                try {
158                        docBuilder = docFactory.newDocumentBuilder();
159                        doc = docBuilder.parse(is);
160                } catch (ParserConfigurationException e) {
161                        // TODO Auto-generated catch block
162                        e.printStackTrace();
163                } catch (SAXException e) {
164                        // TODO Auto-generated catch block
165                        e.printStackTrace();
166                } catch (IOException e) {
167                        // TODO Auto-generated catch block
168                        e.printStackTrace();
169                }       
170                return doc;
171        }
172        public String getDocumentData(InputStream is, String expression){
173                String sessionid = "";
174       
175                XPathFactory factory = XPathFactory.newInstance();
176            XPath xpath = factory.newXPath();
177            XPathExpression expr;
178                try {
179                        expr = xpath.compile(expression);
180                        sessionid = (String) expr.evaluate(getDocument(is), XPathConstants.STRING);
181                } catch (XPathExpressionException e) {
182                        // TODO Auto-generated catch block
183                        e.printStackTrace();
184                }
185                       
186                return sessionid;
187               
188        }
189        */
190        /*
191        public InputStream getSourcePz2() throws Exception {
192               
193                InputStream is = null;
194                // TODO param settings
195                //init
196                setCommand("init");
197                String sessionID = getDocumentData(this.getSourceStream(), "//init/session");
198                // get result
199                setCommand("search");
200                setSessionID(sessionID);
201                setQuery(this.getSquery());
202                String ok = getDocumentData(this.getSourceStream(),"//search/status");
203               
204                String activeclients = "1";
205                setCommand("show");
206                setSort("relevance");
207                setBlock("1");
208                //TODO timeout  settings
209                long startMillis = System.currentTimeMillis();
210                long timeout = 0;
211                if (this.getWPOption("pazpartimeout") == null){
212                        timeout = 1000;
213                } else {
214                        timeout = Integer.parseInt(this.getWPOption("pazpartimeout"));
215                }
216                while (Integer.parseInt(activeclients) > 0){
217                        //activeclients = getDocumentData(this.getSourceStream(), "//show/activeclients");
218                        is = this.getSourceStream();
219                        activeclients = getDocumentData(is, "//show/activeclients");
220                        Thread.sleep(1000);
221                        Admin.notifyUser("ActiveClients: " + activeclients, Admin.DEBUG);
222                        if ((System.currentTimeMillis() - startMillis)/1000 > timeout) {
223                                break;
224                        }
225                }
226                is = this.getSourceStream();
227                return is;
228        }
229*/
230       
231}
Note: See TracBrowser for help on using the repository browser.