source: SRUAggregator/tags/SRUAggregator-1.2/src/main/java/eu/clarin/sru/fcs/aggregator/data/CenterRegistry.java @ 2740

Last change on this file since 2740 was 2740, checked in by yana, 11 years ago

deployed version: supports endpoint testing mode and saving search result into personal workspace

File size: 5.6 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package eu.clarin.sru.fcs.aggregator.data;
6
7import eu.clarin.sru.fcs.aggregator.sparam.CorpusTreeNode;
8import java.io.IOException;
9import java.io.InputStream;
10import java.net.URL;
11import java.net.URLConnection;
12import java.util.ArrayList;
13import java.util.List;
14import java.util.logging.Level;
15import java.util.logging.Logger;
16import javax.xml.parsers.DocumentBuilder;
17import javax.xml.parsers.DocumentBuilderFactory;
18import javax.xml.xpath.XPath;
19import javax.xml.xpath.XPathConstants;
20import javax.xml.xpath.XPathExpressionException;
21import javax.xml.xpath.XPathFactory;
22import org.w3c.dom.NodeList;
23
24/**
25 * Center registry node. Its children are centers (institutions).
26 *
27 * @author Yana Panchenko
28 */
29public class CenterRegistry implements CorpusTreeNode {
30
31    private static final Logger logger = Logger.getLogger(CenterRegistry.class.getName());
32    private static final String crStartpoint = "http://130.183.206.32/restxml/";
33    //https://centerregistry-clarin.esc.rzg.mpg.de/restxml/
34    private boolean hasChildrenLoaded = false;
35    private List<Institution> centers = new ArrayList<Institution>();
36
37    @Override
38    public boolean hasChildrenLoaded() {
39        return hasChildrenLoaded;
40    }
41
42    @Override
43    public void loadChildren() {
44        loadChildren(false);
45    }
46
47    public void loadChildren(boolean testingMode) {
48       
49        if (hasChildrenLoaded) {
50            return;
51        }
52        hasChildrenLoaded = true;
53
54        if (testingMode) {
55            loadInstitutionForTesting();
56        } else {
57            loadInstitutionsFromCR();
58        }
59
60        logger.log(Level.FINE, "Number of Centers: {0}", centers.size());
61
62    }
63
64    @Override
65    public List<? extends CorpusTreeNode> getChildren() {
66        loadChildren();
67        return centers;
68    }
69
70    @Override
71    public CorpusTreeNode getChild(int index) {
72        loadChildren();
73        if (index >= centers.size()) {
74            return null;
75        }
76        return centers.get(index);
77    }
78
79    public void addForeignPoint(String endpointUrl, String institutionLink) {
80        //TODO: ask what functionality is required here??
81//        boolean added = false;
82//        for (Institution center : this.centers) {
83//            if (center.getLink().equals(institutionLink)) {
84//                EndpointY ep = new EndpointY(endpointUrl, center);
85//                center.loadChildren();
86//                //center.loadChildren();
87//                //ep.loadChildren();
88//            }
89//        }
90//        if (!added) {
91//            Institution institution = new Institution("unknown", institutionLink);
92//            this.centers.add(institution);
93//            //EndpointY ep = new EndpointY(endpointUrl, institution);
94//            //this.loadChildren();
95//            //institution.loadChildren();
96//        }
97    }
98
99    public static NodeList evaluateXPath(String statement, org.w3c.dom.Document domtree) {
100        NodeList result = null;
101
102        XPath xpath = XPathFactory.newInstance().newXPath();
103        try {
104            result = (NodeList) xpath.evaluate(statement, domtree, XPathConstants.NODESET);
105        } catch (XPathExpressionException ex) {
106            logger.log(Level.SEVERE, "Error parsing XML: ", statement);
107        }
108        return result;
109    }
110
111    //TODO change to use Alex binding for that...
112    private void loadInstitutionsFromCR() {
113        InputStream is = null;
114        URL u;
115        NodeList instituteNames;
116        NodeList institutionsUrls;
117        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
118        DocumentBuilder builder;
119        URLConnection urlConn;
120        try {
121            u = new URL(crStartpoint);
122            urlConn = u.openConnection();
123
124            //HttpsURLConnection urlConn = (HttpsURLConnection) u.openConnection();
125
126            urlConn.setConnectTimeout(5000);
127            urlConn.setReadTimeout(15000);
128            urlConn.setAllowUserInteraction(false);
129
130            is = urlConn.getInputStream();
131
132
133            //InputStream is = u.openStream();
134
135            builder = factory.newDocumentBuilder();
136            org.w3c.dom.Document document = builder.parse(is);
137
138
139            instituteNames = evaluateXPath("//Centername", document);
140            institutionsUrls = evaluateXPath("//Center_id_link", document);
141
142            for (int i = 0; i < institutionsUrls.getLength(); i++) {
143                String institutionUrl = institutionsUrls.item(i).getTextContent();
144                String institutionName = instituteNames.item(i).getTextContent();
145                Institution institution = new Institution(institutionName, institutionUrl);
146                // display in the tree only those institutions that have CQL endpoints:
147                if (!institution.getChildren().isEmpty()) {
148                    centers.add(institution);
149                }
150            }
151
152        } catch (Exception ex) {
153            logger.log(Level.SEVERE, "Error accessing central registry information {0} {1}", new String[]{ex.getClass().getName(), ex.getMessage()});
154        } finally {
155            if (is != null) {
156                try {
157                    is.close();
158                } catch (IOException ex) {
159                    logger.log(Level.SEVERE, ex.getMessage());
160                }
161            }
162        }
163    }
164
165    private void loadInstitutionForTesting() {
166        String institutionUrl = "http://www.example.org";
167        String institutionName = "Institution for Testing";
168        Institution institution = new InstitutionForTesting(institutionName, institutionUrl);
169        if (!institution.getChildren().isEmpty()) {
170            centers.add(institution);
171        }
172    }
173}
Note: See TracBrowser for help on using the repository browser.