View Javadoc
1   package org.kuali.ole.select.gokb.util;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.GOKBConnectionReader;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.rice.core.api.config.property.ConfigContext;
7   import org.kuali.rice.core.api.exception.RiceRuntimeException;
8   import org.kuali.rice.core.api.util.xml.XmlHelper;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  import org.w3c.dom.Document;
12  import org.w3c.dom.Node;
13  import org.w3c.dom.NodeList;
14  
15  import javax.xml.xpath.XPath;
16  import javax.xml.xpath.XPathConstants;
17  import javax.xml.xpath.XPathFactory;
18  import java.io.ByteArrayInputStream;
19  import java.sql.Date;
20  import java.sql.Timestamp;
21  import java.text.DateFormat;
22  import java.text.SimpleDateFormat;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.TimeZone;
26  
27  /**
28   * Created by rajeshbabuk on 12/18/14.
29   */
30  public class OleGokbXmlUtil {
31  
32      private static final Logger LOG = LoggerFactory.getLogger(OleGokbXmlUtil.class);
33      //private static final String COMMON_URL = "https://gokb.k-int.com/gokb/oai/";
34      //private static final String COMMON_URL = "https://test-gokb.kuali.org/gokb/oai/";
35      private static String COMMON_URL=getGokbUrl();
36      private static final String PACKAGE_URL = "packages?verb=";
37      private static final String TITLE_URL = "titles?verb=";
38      private static final String PLATFORM_URL = "platforms?verb=";
39      private static final String ORGS_URL = "orgs?verb=";
40      private static final String LIST_RECORDS_URL = "listRecords&metadataPrefix=gokb&resumptionToken=||";
41      private static final String GET_IDENTIFIER_RECORDS_URL = "getRecord&metadataPrefix=gokb&identifier=";
42      private static final String LIST_IDENTIFIERS_URL = "ListIdentifiers&metadataPrefix=gokb&resumptionToken=||";
43      private static final String FROM_URL = "&from=";
44      private static final String PIPE = "|";
45      private static final String GOKB = "gokb";
46      private static final String COMPLETE_LIST_SIZE = "//resumptionToken/@completeListSize";
47      private static XPath xPath = XPathFactory.newInstance().newXPath();
48      /**
49       * This method returns the package response from the url.
50       * @param endIndex
51       * @return
52       */
53      public static String getPackageResponseXmlFromGokb(int endIndex) {
54          // URL : https://gokb.k-int.com/gokb/oai/packages?verb=listRecords&metadataPrefix=gokb&resumptionToken=||3|gokb
55          String url = COMMON_URL + PACKAGE_URL + LIST_RECORDS_URL + endIndex + PIPE + GOKB;
56          String responseXml = null;
57          try {
58              responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
59          } catch (Exception e) {
60              LOG.error("Exception while getting package response from url", e);
61          }
62          return responseXml;
63      }
64  
65      /**
66       * This method returns the title response from the url.
67       * @param endIndex
68       * @return
69       */
70      public static String getTitleResponseXmlFromGokb(int endIndex) {
71          // URL : https://gokb.k-int.com/gokb/oai/titles?verb=listRecords&metadataPrefix=gokb&resumptionToken=||6|gokb
72          String url = COMMON_URL + TITLE_URL + LIST_RECORDS_URL + endIndex + PIPE + GOKB;
73          String responseXml = null;
74          try {
75              responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
76          } catch (Exception e) {
77              LOG.error("Exception while getting title response from url", e);
78          }
79          return responseXml;
80      }
81  
82      /**
83       * This method returns the platform response from the url.
84       * @param endIndex
85       * @return
86       */
87      public static String getPlatformResponseXmlFromGokb(int endIndex) {
88          // URL : https://gokb.k-int.com/gokb/oai/platforms?verb=listRecords&metadataPrefix=gokb&resumptionToken=||6|gokb
89          String url = COMMON_URL + PLATFORM_URL + LIST_RECORDS_URL + endIndex + PIPE + GOKB;
90          String responseXml = null;
91          try {
92              responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
93          } catch (Exception e) {
94              LOG.error("Exception while getting platform response from url", e);
95          }
96          return responseXml;
97      }
98  
99      /**
100      * This method returns the organization response from the url.
101      * @param endIndex
102      * @return
103      */
104     public static String getOrgsResponseXmlFromGokb(int endIndex) {
105         // URL : https://gokb.k-int.com/gokb/oai/orgs?verb=listRecords&metadataPrefix=gokb&resumptionToken=||6|gokb
106         String url = COMMON_URL + ORGS_URL + LIST_RECORDS_URL + endIndex + PIPE + GOKB;
107         String responseXml = null;
108         try {
109             responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
110         } catch (Exception e) {
111             LOG.error("Exception while getting orgs response from url", e);
112         }
113         return responseXml;
114     }
115 
116     /**
117      * This method returns the package response based on the last update time from the url.
118      * @param lastUpdatedTime
119      * @param endIndex
120      * @return
121      */
122     public static String getPackageResponseXmlFromGokb(String lastUpdatedTime, int endIndex) {
123         // URL : https://gokb.k-int.com/gokb/oai/packages?verb=ListIdentifiers&metadataPrefix=gokb&resumptionToken=||6&from=2014-12-01T20:40:01Z
124         String url = COMMON_URL + PACKAGE_URL + LIST_IDENTIFIERS_URL + endIndex + FROM_URL + lastUpdatedTime;
125         String responseXml = null;
126         try {
127             responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
128         } catch (Exception e) {
129             LOG.error("Exception while getting package response from url", e);
130         }
131         return responseXml;
132     }
133 
134     /**
135      * This method returns the platfrom response based on the last update time from the url.
136      * @param lastUpdatedTime
137      * @param endIndex
138      * @return
139      */
140     public static String getPlatformResponseXmlFromGokb(String lastUpdatedTime, int endIndex) {
141         // URL : https://gokb.k-int.com/gokb/oai/platforms?verb=ListIdentifiers&metadataPrefix=gokb&resumptionToken=||6&from=2014-12-01T20:40:01Z
142         String url = COMMON_URL + PLATFORM_URL + LIST_IDENTIFIERS_URL + endIndex + FROM_URL + lastUpdatedTime;
143         String responseXml = null;
144         try {
145             responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
146         } catch (Exception e) {
147             LOG.error("Exception while getting platform response from url", e);
148         }
149         return responseXml;
150     }
151 
152     /**
153      * This method returns the title response based on the last update time from the url.
154      * @param lastUpdatedTime
155      * @param endIndex
156      * @return
157      */
158     public static String getTitleResponseXmlFromGokb(String lastUpdatedTime, int endIndex) {
159         // URL : https://gokb.k-int.com/gokb/oai/titles?verb=ListIdentifiers&metadataPrefix=gokb&resumptionToken=||6&from=2014-12-01T20:40:01Z
160         String url = COMMON_URL + TITLE_URL + LIST_IDENTIFIERS_URL + endIndex + FROM_URL + lastUpdatedTime;
161         String responseXml = null;
162         try {
163             responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
164         } catch (Exception e) {
165             LOG.error("Exception while getting title response from url", e);
166         }
167         return responseXml;
168     }
169 
170     /**
171      * This method returns the organization response based on the last update time from the url.
172      * @param lastUpdatedTime
173      * @param endIndex
174      * @return
175      */
176     public static String getOrgsResponseXmlFromGokb(String lastUpdatedTime, int endIndex) {
177         // URL : https://gokb.k-int.com/gokb/oai/orgs?verb=ListIdentifiers&metadataPrefix=gokb&resumptionToken=||6&from=2014-12-01T20:40:01Z
178         String url = COMMON_URL + ORGS_URL + LIST_IDENTIFIERS_URL + endIndex + FROM_URL + lastUpdatedTime;
179         String responseXml = null;
180         try {
181             responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
182         } catch (Exception e) {
183             LOG.error("Exception while getting orgs response from url", e);
184         }
185         return responseXml;
186     }
187 
188 
189     /**
190      * This method returns the package response based on the identifier from the url.
191      * @param identifier
192      * @return
193      */
194     public static String getPackageResponseXmlFromGokb(String identifier) {
195         // URL : https://gokb.k-int.com/gokb/oai/packages?verb=getRecord&metadataPrefix=gokb&identifier=org.gokb.cred.Package:37779
196         String url = COMMON_URL + PACKAGE_URL + GET_IDENTIFIER_RECORDS_URL + identifier;
197         String responseXml = null;
198         try {
199             responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
200         } catch (Exception e) {
201             LOG.error("Exception while getting package identifier response from url", e);
202         }
203         return responseXml;
204     }
205 
206     /**
207      * This method returns the platform response based on the identifier from the url.
208      * @param identifier
209      * @return
210      */
211     public static String getPlatformResponseXmlFromGokb(String identifier) {
212         // URL : https://gokb.k-int.com/gokb/oai/platforms?verb=getRecord&metadataPrefix=gokb&identifier=org.gokb.cred.Platform:37779
213         String url = COMMON_URL + PLATFORM_URL + GET_IDENTIFIER_RECORDS_URL + identifier;
214         String responseXml = null;
215         try {
216             responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
217         } catch (Exception e) {
218             LOG.error("Exception while getting platform identifier response from url", e);
219         }
220         return responseXml;
221     }
222 
223     /**
224      * This method returns the title response based on the identifier from the url.
225      * @param identifier
226      * @return
227      */
228     public static String getTitleResponseXmlFromGokb(String identifier) {
229         // URL : https://gokb.k-int.com/gokb/oai/titles?verb=getRecord&metadataPrefix=gokb&identifier=org.gokb.cred.Title:37779
230         String url = COMMON_URL + TITLE_URL + GET_IDENTIFIER_RECORDS_URL + identifier;
231         String responseXml = null;
232         try {
233             responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
234         } catch (Exception e) {
235             LOG.error("Exception while getting title identifier response from url", e);
236         }
237         return responseXml;
238     }
239 
240     /**
241      * This method returns the organization response based on the identifier from the url.
242      * @param identifier
243      * @return
244      */
245     public static String getOrgsResponseXmlFromGokb(String identifier) {
246         // URL : https://gokb.k-int.com/gokb/oai/orgs?verb=getRecord&metadataPrefix=gokb&identifier=org.gokb.cred.org:37779
247         String url = COMMON_URL + ORGS_URL + GET_IDENTIFIER_RECORDS_URL + identifier;
248         String responseXml = null;
249         try {
250             responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
251         } catch (Exception e) {
252             LOG.error("Exception while getting orgs identifier response from url", e);
253         }
254         return responseXml;
255     }
256 
257     /**
258      * This method returns ElementValue using docContent and xpathExpression..
259      * @param docContent
260      * @param xpathExpression
261      * @return value
262      */
263     public static NodeList getElementNodeList(String docContent, String xpathExpression) {
264         try {
265             Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
266             NodeList nodeList = (NodeList) xPath.compile(xpathExpression).evaluate(document, XPathConstants.NODESET);
267             return nodeList;
268 
269         } catch (Exception e) {
270             throw new RiceRuntimeException();
271         }
272     }
273 
274 
275     /**
276      * This method returns ElementValue using docContent and xpathExpression..
277      * @param docContent
278      * @param xpathExpression
279      * @return value
280      */
281     private static String getElementValue(String docContent, String xpathExpression) {
282         try {
283             Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
284             String value = (String) xPath.evaluate(xpathExpression, document, XPathConstants.STRING);
285             return value;
286         } catch (Exception e) {
287             throw new RiceRuntimeException();
288         }
289     }
290 
291     /**
292      * This method returns the page size from the response.
293      * @param responseXml
294      * @return
295      */
296     public static int getPageSizeFromResponse(String responseXml) {
297         NodeList resumptionTokenNode = getElementNodeList(responseXml, OLEConstants.OleGokb.RESUMPTION_TOKEN_EXP);
298         if (null != resumptionTokenNode && null != resumptionTokenNode.item(0)) {
299             String resumptionTokenValue = StringUtils.substringAfter(resumptionTokenNode.item(0).getTextContent(), PIPE + PIPE);
300             if (resumptionTokenValue.contains(PIPE + GOKB)) {
301                 return Integer.parseInt(StringUtils.substringBefore(resumptionTokenValue, PIPE + GOKB));
302             } else {
303                 return Integer.parseInt(resumptionTokenValue);
304             }
305         }
306         return 0;
307     }
308 
309     /**
310      * This method returns the complete list size from the response.
311      * @param results
312      * @return
313      */
314     public static int getCompleteListSize(String results) {
315         String completeListSize = getElementValue(results, COMPLETE_LIST_SIZE);
316         if (StringUtils.isNotBlank(completeListSize)) {
317             return Integer.parseInt(completeListSize);
318         } else {
319             return 0;
320         }
321     }
322 
323     /**
324      * This method returns the update dates from the response.
325      * @param results
326      * @return
327      */
328     public static List<String> getUpdatedDates(String results) {
329         List<String> updatedDates = new ArrayList<>();
330         NodeList records = OleGokbXmlUtil.getElementNodeList(results, OLEConstants.OleGokb.RECORD_XPATH_EXP);
331         for (int i = 0; i < records.getLength(); i++) {
332             NodeList nodeList = records.item(i).getChildNodes();
333             for (int j = 0; j < nodeList.getLength(); j++) {
334                 if (nodeList.item(j).getNodeName().equalsIgnoreCase(OLEConstants.OleGokb.HEADER)) {
335                     NodeList headerChildNodes = nodeList.item(j).getChildNodes();
336                     for (int k = 0; k < headerChildNodes.getLength(); k++) {
337                         if (headerChildNodes.item(k).getNodeName().equalsIgnoreCase(OLEConstants.OleGokb.DATE_STAMP)) {
338                             updatedDates.add(headerChildNodes.item(k).getTextContent());
339                         }
340                     }
341                 }
342             }
343         }
344         return updatedDates;
345     }
346 
347     /**
348      * This method returns the UTC date format.
349      * @return
350      */
351     private static DateFormat getUTCDateFormat() {
352         DateFormat df = new SimpleDateFormat(OLEConstants.OleGokb.UTC_DATE_FORMAT);
353         df.setTimeZone(TimeZone.getTimeZone(OLEConstants.OleGokb.UTC_TIME_ZONE));
354         return df;
355     }
356 
357     /**
358      * This method returns the timestamp from string.
359      * @param date
360      * @return
361      */
362     public static Timestamp getTimeStampFromString(String date) {
363         Timestamp timestamp = null;
364         try {
365             if (StringUtils.isNotBlank(date)) {
366                 timestamp = new Timestamp(getUTCDateFormat().parse(date).getTime());
367             }
368         } catch (Exception e) {
369             LOG.error("Exception while parsing date" + e);
370         }
371         return timestamp;
372     }
373 
374     /**
375      * This method returns string from timestamp.
376      * @param timestamp
377      * @return
378      */
379     public static String getStringFromTimeStamp(Timestamp timestamp) {
380         String utcTime = null;
381         try {
382             if (timestamp != null) {
383                 Date date = new Date(timestamp.getTime());
384                 utcTime = getUTCDateFormat().format(date);
385             }
386         } catch (Exception e) {
387             LOG.error("Exception while formatting date" + e);
388         }
389         return utcTime;
390     }
391 
392     /**
393      * This method returns identifier from the header.
394      * @param headerNode
395      * @return
396      */
397     public static String getIdentifierFromHeader(Node headerNode) {
398         return headerNode.getChildNodes().item(0).getTextContent();
399     }
400 
401     public static String getGokbUrl(){
402         if(ConfigContext.getCurrentContextConfig() != null){
403            return ConfigContext.getCurrentContextConfig().getProperty("gokb.url");
404         }else
405             return null;
406     }
407 
408     public static void setGokbUrl(String url){
409           COMMON_URL=url;
410     }
411 
412 }