source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/SRUProxyAction.java @ 1184

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

SRU - basics - action

File size: 5.5 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.BufferedInputStream;
4import java.io.BufferedReader;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.InputStreamReader;
10import java.net.MalformedURLException;
11import java.net.URISyntaxException;
12import java.net.URL;
13import java.util.HashMap;
14
15import javax.servlet.http.HttpServletRequest;
16import javax.servlet.http.HttpSession;
17import javax.xml.transform.TransformerException;
18
19import net.sf.json.JSON;
20import net.sf.json.JSONArray;
21import net.sf.json.JSONObject;
22import net.sf.json.JSONSerializer;
23
24
25import org.apache.commons.io.IOUtils;
26import org.apache.commons.lang.StringEscapeUtils;
27import org.apache.struts2.interceptor.ServletRequestAware;
28
29import com.opensymphony.xwork2.ActionSupport;
30
31/**
32 * main Struts 2 controller
33 * responds to requests (collections, model, recordset)
34 * by dispatching the requests to appropriate internal methods and filling the inputStream with the result
35 * 
36 * @author vronk
37 *
38 */
39public class SRUProxyAction extends ActionSupport 
40{
41
42        private static final long serialVersionUID = 1L;
43       
44        /**
45         * Properties to be filled by Struts with according request-parameters
46         */     
47        private String version;
48        private String operation;
49        private String query;
50        private int startRecord;
51        private int maximumRecords;
52        private String recordPacking;
53        private String recordSchema;
54        private String recordXPath;
55        private int resultSetTTL;
56        private String sortKeys;
57        private String stylesheet;     
58        private String extraRequestData;
59
60        private MDRepoProxyAction mdrepoproxyaction;
61        private InputStream resultStream;
62       
63        private final static HashMap<String,String> hash_operations = new HashMap<String,String>();
64         static
65         {       
66                 hash_operations.put("searchRetrieve", "recordset");
67         } 
68         
69         public SRUProxyAction(){
70                 super();
71                 initialize();
72         }
73         
74         protected void  initialize(){
75                 //repository = 1;
76         }
77         
78         public InputStream getResultStream() {
79             return resultStream;
80         }
81         public void setResultStream(InputStream _resultStream){
82                resultStream = _resultStream;
83         }
84       
85        public String getVersion() {
86                return version;
87        }
88
89        public void setVersion(String version) {
90                this.version = version;
91        }
92
93        public String getOperation() {
94                return operation;
95        }
96
97        public void setOperation(String operation) {
98                this.operation = operation;
99        }
100       
101        public String getQuery() {
102                return query;
103        }
104
105        public void setQuery(String query) {
106                this.query = query;
107        }
108       
109        public int getStartRecord() {
110                return startRecord;
111        }
112
113        public void setStartRecord(int startRecord) {
114                this.startRecord = startRecord;
115        }
116        public int getMaximumRecords() {
117                return maximumRecords;
118        }
119
120        public void setMaximumRecords(int maximumRecords) {
121                this.maximumRecords = maximumRecords;
122        }
123       
124        public String getRecordPacking() {
125                return recordPacking;
126        }
127
128        public void setRecordPacking(String recordPacking) {
129                this.recordPacking = recordPacking;
130        }
131
132        public String getRecordSchema() {
133                return recordSchema;
134        }
135
136        public void setRecordSchema(String recordSchema) {
137                this.recordSchema = recordSchema;
138        }
139
140        public String getRecordXPath() {
141                return recordXPath;
142        }
143
144        public void setRecordXPath(String recordXPath) {
145                this.recordXPath = recordXPath;
146        }
147
148        public int getResultSetTTL() {
149                return resultSetTTL;
150        }
151
152        public void setResultSetTTL(int resultSetTTL) {
153                this.resultSetTTL = resultSetTTL;
154        }
155       
156        public String getSortKeys() {
157                return sortKeys;
158        }
159
160        public void setSortKeys(String sortKeys) {
161                this.sortKeys = sortKeys;
162        }
163               
164        public String getStylesheet() {
165                return stylesheet;
166        }
167
168        public void setStylesheet(String stylesheet) {
169                this.stylesheet = stylesheet;
170        }
171       
172        public String getExtraRequestData() {
173                return extraRequestData;
174        }
175
176        public void setExtraRequestData(String extraRequestData) {
177                this.extraRequestData = extraRequestData;
178        }
179
180
181        /**
182         * primitive identification of the target-proxy
183         * base for finding the right base_url in the props
184         * subclass has to override with its specific proxykey
185         * @return
186         */
187        public String getProxyKey() {
188                return "";
189        }
190       
191        public void prepare(){
192                mdrepoproxyaction = new MDRepoProxyAction();
193                mdrepoproxyaction.setRepository(2);
194               
195                mdrepoproxyaction.setActionkey(hash_operations.get(this.getOperation()));
196                mdrepoproxyaction.setSquery("");
197                mdrepoproxyaction.setQ(getQuery());
198                mdrepoproxyaction.setFormat("xml");
199               
200                if (getStartRecord() > 0 ){
201                        mdrepoproxyaction.setStartItem(Integer.toString(getStartRecord()));
202                }
203                if (getMaximumRecords() > 0 ) {
204                        mdrepoproxyaction.setMaximumItems(Integer.toString(this.getMaximumRecords()));
205                }
206               
207        }
208        public static String inputStreamAsString(InputStream stream)
209                throws IOException {
210                BufferedReader br = new BufferedReader(new InputStreamReader(stream));
211                StringBuilder sb = new StringBuilder();
212                String line = null;
213
214                while ((line = br.readLine()) != null) {
215                sb.append(line + "\n");
216                }
217
218                br.close();
219                return sb.toString();
220        }
221
222        public void postprocess(){
223                //recordPacking
224                if (this.getRecordPacking() != null){
225                        if (this.getRecordPacking().toLowerCase().equals("string")){
226                                try {
227                                        resultStream = IOUtils.toInputStream(StringEscapeUtils.escapeXml(inputStreamAsString(resultStream)));
228                                } catch (IOException e) {
229                                        // TODO Auto-generated catch block
230                                        e.printStackTrace();
231                                }
232
233                        }
234                }
235        }
236        /**
237         * default Action method
238         */
239        public String execute() throws Exception {
240
241                prepare();
242                if (this.mdrepoproxyaction.execute() == SUCCESS){
243                        resultStream = this.mdrepoproxyaction.getResultStream();
244                        postprocess();
245                        return SUCCESS;
246                }
247                return ERROR;
248        }
249
250
251}
Note: See TracBrowser for help on using the repository browser.