source: MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/proxy/BasicProxy.java @ 1907

Last change on this file since 1907 was 1907, checked in by gaba, 12 years ago

x-context replaced $repository, fcs/xsl's used

File size: 3.7 KB
Line 
1package eu.clarin.cmdi.mdservice.proxy;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.net.MalformedURLException;
6import java.net.URL;
7import java.net.URLConnection;
8import java.util.Map;
9
10import org.apache.log4j.Logger;
11import eu.clarin.cmdi.mdservice.action.GenericAction;
12import eu.clarin.cmdi.mdservice.internal.CQLParseException;
13import eu.clarin.cmdi.mdservice.internal.NoStylesheetException;
14import eu.clarin.cmdi.mdservice.internal.Utils;
15
16
17/**
18 * Base class for implementation of proxies to target systems.
19 * This is now (after rework 2011-09) decoupled from the called Action.
20 *
21 * Defines the methods used for retrieving the data
22 * and provides basic implementation where possible.
23 *
24 * Tries to get the base_uri from Config (mdservice.properties) based on action_key
25 * 
26 * @author vronk
27 *
28 */
29public class BasicProxy implements ProxyInterface{
30
31        private static final long serialVersionUID = 1L;
32        private static Logger log = Logger.getLogger("BasicProxy");
33 
34        private String proxy_key = "basic";
35       
36        private GenericAction source_action;
37       
38        public GenericAction getSourceAction() {
39                        return source_action;
40        }
41       
42        @Override
43        public void setSourceAction(GenericAction action) {             
44                source_action = action;
45               
46        }
47       
48        public void addParam (String key, String value){
49                 getSourceAction().addParam(key, value);
50         }
51       
52        public Map<String,String[]> getParams() {
53                return getSourceAction().getParams();
54        }
55       
56        /**
57         * Convenience function to access the request-parameters in the Action 
58         * @param key
59         * @return
60         */
61        public String getParam(String key) {
62                return getSourceAction().getParam(key);
63        }
64       
65        public void checkParams() {
66                // required Params
67/*
68                //if (getParam("q").equals("")) {
69                //      getSourceAction().getDiagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "q");
70                //}
71                if (getParams().get("format") == null){
72                        if (getSourceAction().getFormat() == null) {
73                                addParam("format","xml");
74                        } else if (getSourceAction().getFormat().equals("")){
75                                addParam("format","xml");
76                        } else {
77                                addParam("format",getSourceAction().getFormat());
78                        }
79                }
80               
81                addParam("fullformat",getSourceAction().getFullFormat());
82                */
83        }
84       
85        /**
86         * identification of the target-proxy (type)
87         * base for finding the right base_url in the props
88         * subclass has to override with its specific proxykey
89         * @return
90         */
91        public String getProxyKey() {                           
92                        return proxy_key;               
93        }
94
95        /**
96         * Get the base_uri based on the action-key
97         */
98               
99        public String getBaseURI() {           
100                //String uri = Utils.getConfig).getProperty(getSourceAction().getActionkey() + ".uri");
101                String uri = Utils.getConfig(getSourceAction().getActionkey() + ".uri");
102                //String uri = Termset.getTermsetAttr(getSourceAction().getActionkey(), "url");
103                return uri;
104        }
105
106        /**
107         * Provides the base-URL specific to given target repository or registry
108         * @return
109         * @throws MalformedURLException
110         */     
111        public URL getBaseURL() throws MalformedURLException {         
112                URL base_url = new URL(getBaseURI());           
113                return base_url;
114        }
115       
116       
117        /**
118         * uses base_url + url_pattern (parametrized by actionkey) to form a url
119         * @return the full target-request URL to be sent to the target repository
120         * @throws CQLParseException
121         * @throws Exception
122         */     
123        public URL getTargetRequest() throws IOException, CQLParseException {
124                 
125                URL targetURL =new URL(getBaseURI() + this.getSourceAction().getTargetRequestParams());
126               
127                log.debug("getTargetRequest().targetURL:" + targetURL);
128            return targetURL;
129               
130        }
131
132        public InputStream getSourceStream() throws IOException, NoStylesheetException, CQLParseException {
133                URLConnection urlConnection = getTargetRequest().openConnection();
134                getSourceAction().setRequestProperties(urlConnection);
135                return urlConnection.getInputStream();//.openStream();         
136        }
137
138       
139
140}       
Note: See TracBrowser for help on using the repository browser.