source: MDService2/branches/MDService_simple2/src/eu/clarin/cmdi/mdservice/action/DCRProxyAction.java @ 1511

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

brutal refactoring,
added new packages: proxies, internal,
very much restructured Action hierarchy.
Introduced division Action/Proxy?.
This is a first compiling version.
it's total mess.

File size: 4.6 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.net.MalformedURLException;
6import java.net.URL;
7import java.net.URLConnection;
8import java.util.HashMap;
9
10import javax.xml.transform.stream.StreamSource;
11import javax.xml.xpath.XPath;
12import javax.xml.xpath.XPathConstants;
13import javax.xml.xpath.XPathExpression;
14import javax.xml.xpath.XPathExpressionException;
15import javax.xml.xpath.XPathFactory;
16
17import org.apache.log4j.Logger;
18import org.w3c.dom.Document;
19import org.w3c.dom.Node;
20import org.w3c.dom.NodeList;
21
22import eu.clarin.cmdi.mdservice.internal.Admin;
23import eu.clarin.cmdi.mdservice.model.Termset;
24
25/**
26 * Acts as a Struts 2 controller
27 * serves the requests regarding DataCategories
28 * it passes them further (Proxy) to the Data Category Registry.
29 *   
30 * @author vronk
31 *
32 */
33
34public class DCRProxyAction extends GenericProxyAction {
35
36        private String proxy_key = "dcregistry";
37        private static Logger log = Logger.getLogger("DCRProxyAction");
38       
39        private String dcrid;
40       
41        public static String ALL = "all";
42        public static String DATCAT = "datcat";
43        public static String DATCATS = "datcats";
44       
45        private static final long serialVersionUID = 1L;
46       
47       
48        public String getDcrid() {
49                return dcrid;
50        }
51
52        public void setDcrid(String dcrid) {
53                this.dcrid = dcrid;
54        }
55
56        public Termset getDCR() {               
57                String dcr_id="";
58                if (getQ()!=null && !getQ().equals("")) dcr_id = getQ();
59                // override with dcrid-param
60                if (getDcrid()!=null && !getDcrid().equals("")) dcr_id = getDcrid();                   
61
62                Termset res = (Termset) Termset.getTermset(dcr_id);
63                if (res==null)          Admin.notifyUser("getDCR(): no termset found for:" + dcr_id);
64                //else
65                        //Admin.notifyUser("getDCR():" + res.getAttr("id") + ":" + res.getAttr("url") );
66               
67                return res;
68        }
69       
70        public String getDCRAttr(String key) { 
71                Termset dcr = getDCR();
72                String val=""; 
73                if (dcr!=null) val= dcr.getAttr(key);
74                 return val;
75        }
76       
77        @Override
78        public String getProxyKey() {
79                return proxy_key;
80        }
81         
82        public String getFullFormat() {
83                Termset dcr = getDCR();
84                String srcformat="", srctype=""; 
85                if (dcr!=null){ 
86                                srcformat = dcr.getAttr("format");
87                                srctype = dcr.getAttr("type");
88                } else { 
89                         srcformat = ALL;
90                         srctype = "datcat";
91                }
92                        return srctype + "-" + srcformat  + "2" + getFormat();         
93        }
94       
95        public URL getBaseURL() throws MalformedURLException {
96               
97                if (base_url == null) {
98                        base_url = new URL(getBaseURI());
99                }
100               
101                return base_url;
102        }
103       
104        public String getBaseURI() {           
105                return getDCRAttr("url");
106        }
107       
108        /**
109         * uses base_url and (optionally) id to form a url
110         * no id => ?list all datacategories?
111         * @return the URL to data category xml format
112         * @throws Exception
113         */
114        public URL getTargetRequest() throws IOException {
115
116                URL targetURL = null;
117       
118                //if (getActionkey().equals(DATCATS)) {
119                if ((getQ()!=null && !getQ().equals(""))) {
120                        //targetURL =new URL(getBaseURI() + "?dcif-mode=all&workingLanguage=" + getLang()) ;
121                        targetURL =new URL(getBaseURI() + "?dcif-mode=all&workingLanguage=" + getLang()) ;
122                }
123                /*
124                        if (getQ()==null || getQ().equals("")) {                       
125                                        // get all available datcat-sets from terms_setup.xml
126                               
127                                targetURL =getBaseURL();
128                        } else {
129                                targetURL =new URL( getBaseURL(), "/" + getQ());
130                        }
131                        */
132//              Admin.notifyUser("DCRProxy.targetURL:" + targetURL);
133
134            return targetURL;
135        }
136       
137        public InputStream getSourceStream() throws IOException {
138
139                InputStream resultStream = null; 
140                // if no arguments  - get the local-config file
141                // FIXME: although this should be competence of TermsProxyAction
142                if ((getQ().equals(ALL) || getQ()==null || getQ().equals("")) && (getActionkey().equals(DATCATS))) {
143                        log.debug("serving terms.file:" + Admin.getConfig().getProperty("terms.file"));
144                        resultStream = this.getClass().getClassLoader().getResourceAsStream(Admin.getConfig().getProperty("terms.file"));
145                } else {
146               
147                /* need to set Request-Property: Accept to xml
148                 * for to get dcif/xml from DCR-server by means of content negotiation */
149                URLConnection urlConnection = getTargetRequest().openConnection();
150               
151                if (getDCRAttr("format").equals("dcif")) {             
152                        urlConnection.setRequestProperty("Accept", "application/dcif+xml");
153                        log.debug("setting header Accept:" + urlConnection.getRequestProperty("Accept"));
154                }
155        //urlConnection.setRequestProperty("Accept-Language", getLang());
156       
157        //
158                resultStream = urlConnection.getInputStream();
159                }
160        return resultStream;   
161   
162        }
163
164}
Note: See TracBrowser for help on using the repository browser.