1 | package eu.clarin.cmdi.mdservice.proxy; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.net.MalformedURLException; |
---|
6 | import java.net.URL; |
---|
7 | import java.net.URLConnection; |
---|
8 | import java.util.Map; |
---|
9 | |
---|
10 | import org.apache.log4j.Logger; |
---|
11 | import eu.clarin.cmdi.mdservice.action.GenericAction; |
---|
12 | import eu.clarin.cmdi.mdservice.internal.CQLParseException; |
---|
13 | import eu.clarin.cmdi.mdservice.internal.NoStylesheetException; |
---|
14 | import 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 | */ |
---|
29 | public 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 Map<String,String[]> getParams() { |
---|
49 | return getSourceAction().getParams(); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Convenience function to access the request-parameters in the Action |
---|
54 | * @param key |
---|
55 | * @return |
---|
56 | */ |
---|
57 | public String getParam(String key) { |
---|
58 | return getSourceAction().getParam(key); |
---|
59 | } |
---|
60 | |
---|
61 | public void checkParams() { |
---|
62 | // required Params |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * identification of the target-proxy (type) |
---|
67 | * base for finding the right base_url in the props |
---|
68 | * subclass has to override with its specific proxykey |
---|
69 | * @return |
---|
70 | */ |
---|
71 | public String getProxyKey() { |
---|
72 | return proxy_key; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Get the base_uri based on the action-key |
---|
77 | */ |
---|
78 | |
---|
79 | public String getBaseURI() { |
---|
80 | String uri =Utils.getConfig().getProperty(getSourceAction().getActionkey() + ".uri"); |
---|
81 | //String uri = Termset.getTermsetAttr(getSourceAction().getActionkey(), "url"); |
---|
82 | return uri; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Provides the base-URL specific to given target repository or registry |
---|
87 | * @return |
---|
88 | * @throws MalformedURLException |
---|
89 | */ |
---|
90 | public URL getBaseURL() throws MalformedURLException { |
---|
91 | URL base_url = new URL(getBaseURI()); |
---|
92 | return base_url; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | * uses base_url + url_pattern (parametrized by actionkey) to form a url |
---|
98 | * @return the full target-request URL to be sent to the target repository |
---|
99 | * @throws CQLParseException |
---|
100 | * @throws Exception |
---|
101 | */ |
---|
102 | public URL getTargetRequest() throws IOException, CQLParseException { |
---|
103 | |
---|
104 | URL targetURL =new URL(getBaseURI() + this.getSourceAction().getTargetRequestParams()); |
---|
105 | |
---|
106 | log.debug("getTargetRequest().targetURL:" + targetURL); |
---|
107 | return targetURL; |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | public InputStream getSourceStream() throws IOException, NoStylesheetException, CQLParseException { |
---|
112 | URLConnection urlConnection = getTargetRequest().openConnection(); |
---|
113 | getSourceAction().setRequestProperties(urlConnection); |
---|
114 | return urlConnection.getInputStream();//.openStream(); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | } |
---|