View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.service.impl;
17  
18  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
19  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
20  import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
21  import org.kuali.ole.select.OleSelectConstant;
22  import org.kuali.ole.select.businessobject.BibInfoBean;
23  import org.kuali.ole.select.service.WebClientService;
24  import org.kuali.ole.select.service.impl.exception.DocStoreConnectionException;
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.rice.core.api.util.RiceKeyConstants;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  
29  import java.io.BufferedReader;
30  import java.io.InputStreamReader;
31  import java.io.OutputStreamWriter;
32  import java.net.URL;
33  import java.net.URLConnection;
34  import java.util.HashMap;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  public class WebClientServiceImpl implements WebClientService {
39  
40      private URL url;
41      private URLConnection conn;
42      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WebClientServiceImpl.class);
43  
44  
45      public String sendRequest(String urlString, String contentType, String data) throws Exception {
46          if (LOG.isDebugEnabled()) {
47              LOG.debug("inside sendRequest");
48              LOG.debug("urlString----------------->" + urlString);
49          }
50          String response = "";
51          boolean success = true;
52          // Modified by Aditya sai prasad for jira OLE-2411 starts.
53          try {
54              // OleUrlPing oleUrlPing = new OleUrlPing();
55              // String msg = oleUrlPing.urlPing(urlString);
56              createConnection(urlString, contentType);
57              response = postData(data);
58          } catch (DocStoreConnectionException dsce) {
59              success = false;
60              GlobalVariables.getMessageMap().putError("error.requisition.docstore.connectionError", RiceKeyConstants.ERROR_CUSTOM, "Docstore Server Unavailable.");
61          }
62          
63         /* if(success){
64              createConnection(urlString, contentType);
65              response = postData(data);
66          }*/
67  
68          // Modified by Aditya sai prasad for jira OLE-2411 ends.
69  
70          return response;
71      }
72  
73      private void createConnection(String urlString, String contentType) throws Exception {
74          LOG.debug("inside createConnection");
75          url = new URL(urlString);
76          conn = url.openConnection();
77          conn.setDoInput(true);
78          conn.setDoOutput(true);
79          conn.setUseCaches(false);
80          conn.setRequestProperty("Accept-Charset", "UTF-8");
81          conn.setRequestProperty("Content-Type", contentType);
82  
83      }
84  
85      private String postData(String data) {
86          LOG.debug("inside postData");
87          String response = "";
88          try {
89              OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
90              writer.write(data);
91              writer.flush();
92              // Read response from the input stream.
93              BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
94              String temp;
95              while ((temp = in.readLine()) != null) {
96                  response += temp + "\n";
97              }
98              temp = null;
99              in.close();
100             if (LOG.isDebugEnabled()) {
101                 LOG.debug("response from docstore---------->" + response);
102             }
103         } catch (Exception e) {
104             LOG.error("Exception while connecting to document storage server.", e);
105             throw new DocStoreConnectionException("Error while connecting to document storage server, contact system administrator.", e);
106         }
107         return response;
108     }
109     
110 /*    public String getDataFromResponseXMLForDocStore(String response) throws Exception {
111         String data = null;
112         if(response!=null){
113             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
114             DocumentBuilder db = factory.newDocumentBuilder();
115             InputSource inStream = new InputSource();
116             inStream.setCharacterStream(new StringReader(response));
117             Document doc = db.parse(inStream);
118             NodeList nodeList = doc.getElementsByTagName("response");
119             for (int index = 0; index < nodeList.getLength(); index++) {
120                 Node node = nodeList.item(index);
121                 if (node.getNodeType() == Node.ELEMENT_NODE) {
122                     Element element = (Element) node;
123                     NodeList dataNode = element.getElementsByTagName("item");
124                     for (int iIndex = 0; iIndex < dataNode.getLength(); iIndex++) {
125                         if (dataNode.item(iIndex).getNodeType() == Node.ELEMENT_NODE) {
126                             Element dataElement = (Element) dataNode.item(iIndex);
127                             if ( LOG.isDebugEnabled() ) {
128                                 LOG.info("response data vale = " + dataElement.getFirstChild().getNodeValue().trim());
129                             }
130                             data = dataElement.getFirstChild().getNodeValue().trim();
131                         }
132                     }
133                 }
134             }
135         }
136         return data;
137     }*/
138 
139     public String getDataFromResponseXMLForDocStore(String response, HashMap<String, String> dataMap) throws Exception {
140         StringBuilder uuid = new StringBuilder();
141         //ResponseHandler docStoreResponseHandlerObject = new ResponseHandler();
142         ResponseHandler docStoreResponseHandlerObject = SpringContext.getBean(ResponseHandler.class);
143         Response docStoreResponseObject = docStoreResponseHandlerObject.toObject(response);
144         List<ResponseDocument> requestDocuments = docStoreResponseObject.getDocuments();
145         Iterator docStoreDocumentIterator = requestDocuments.iterator();
146         String instanceType;
147         String uuidType = "";
148         if (dataMap.containsKey(OleSelectConstant.DOC_CATEGORY_TYPE))
149             uuidType = dataMap.get(OleSelectConstant.DOC_CATEGORY_TYPE);
150         while (docStoreDocumentIterator.hasNext()) {
151             ResponseDocument docStoreDocument = (ResponseDocument) docStoreDocumentIterator.next();
152             if (docStoreDocument.getUuid() != null) {
153                 uuid.append(docStoreDocument.getUuid());
154             }
155           /*  List<ResponseDocument> linkedInstanceDocuments = docStoreDocument.getLinkedDocuments();
156             Iterator linkedInstanceDocumentIterator = linkedInstanceDocuments.iterator();
157             while(linkedInstanceDocumentIterator.hasNext()) {
158                 ResponseDocument linkedInstanceDocument = (ResponseDocument)linkedInstanceDocumentIterator.next();
159                 //uuid.append(getUUIDFromLinkedResponseDocument(linkedInstanceDocument, uuidType)); 
160                 //uuid.append(":" + linkedInstanceDocument.getUuid());
161                 String type = linkedInstanceDocument.getType();
162                 String format = linkedInstanceDocument.getFormat();
163                 if(uuidType != null) {
164                     if((uuidType.equalsIgnoreCase(OleSelectConstant.DOC_CATEGORY_TYPE_ITEM)) &&
165                             (type.equalsIgnoreCase(OleSelectConstant.DOCSTORE_TYPE_INSTANCE)) &&
166                             (format.equalsIgnoreCase(OleSelectConstant.DOCSTORE_FORMAT_OLEML))) {
167                         uuid.append(linkedInstanceDocument.getUuid());
168                     }
169                 }
170             }*/
171 
172             if (LOG.isDebugEnabled())
173                 LOG.debug(docStoreDocument.getType() + " uuid---------->" + docStoreDocument.getUuid());
174         }
175         return uuid.toString();
176     }
177 
178     private String getUUIDFromLinkedResponseDocument(ResponseDocument linkedInstanceDocument, String uuidType) {
179         String instanceType = linkedInstanceDocument.getType();
180         List<ResponseDocument> linkedDocuments = linkedInstanceDocument.getLinkedDocuments();
181         Iterator linkedDocumentIterator = linkedDocuments.iterator();
182         String type = "";
183         String format = "";
184         String uuid = "";
185         while (linkedDocumentIterator.hasNext()) {
186             ResponseDocument linkedDocument = (ResponseDocument) linkedDocumentIterator.next();
187             type = linkedDocument.getType();
188             format = linkedDocument.getFormat();
189             if (uuidType != null) {
190                 if ((uuidType.equalsIgnoreCase(OleSelectConstant.DOC_CATEGORY_TYPE_ITEM)) &&
191                         (type.equalsIgnoreCase(OleSelectConstant.DOC_CATEGORY_TYPE_ITEM)) &&
192                         (format.equalsIgnoreCase(OleSelectConstant.DOCSTORE_FORMAT_OLEML))) {
193                     uuid = linkedDocument.getUuid();
194                 }
195             }
196         }
197         return uuid;
198     }
199 
200     public List<BibInfoBean> getUUIDFromResponseXMLForDocStore(String response, HashMap<String, String> dataMap, List<BibInfoBean> bibInfoBeanList) throws Exception {
201         String uuid = null;
202         //ResponseHandler docStoreResponseHandlerObject = new ResponseHandler();
203         ResponseHandler docStoreResponseHandlerObject = SpringContext.getBean(ResponseHandler.class);
204         Response docStoreResponseObject = docStoreResponseHandlerObject.toObject(response);
205         List<ResponseDocument> requestDocuments = docStoreResponseObject.getDocuments();
206         Iterator docStoreDocumentIterator = requestDocuments.iterator();
207         Iterator bibInfoBeanIterator = bibInfoBeanList.iterator();
208         String type;
209         String uuidType = "";
210         if (dataMap.containsKey(OleSelectConstant.DOC_CATEGORY_TYPE))
211             uuidType = dataMap.get(OleSelectConstant.DOC_CATEGORY_TYPE);
212         while (docStoreDocumentIterator.hasNext()) {
213             ResponseDocument docStoreDocument = (ResponseDocument) docStoreDocumentIterator.next();
214             List<ResponseDocument> linkedInstanceDocuments = docStoreDocument.getLinkedDocuments();
215             Iterator linkedInstanceDocumentIterator = linkedInstanceDocuments.iterator();
216             while (linkedInstanceDocumentIterator.hasNext()) {
217                 ResponseDocument linkedInstanceDocument = (ResponseDocument) linkedInstanceDocumentIterator.next();
218                 //uuid = getUUIDFromLinkedResponseDocument(linkedInstanceDocument, uuidType);
219                 String format = linkedInstanceDocument.getFormat();
220                 type = linkedInstanceDocument.getType();
221                 ;
222                 if ((uuidType.equalsIgnoreCase(OleSelectConstant.DOC_CATEGORY_TYPE_ITEM)) &&
223                         (type.equalsIgnoreCase(OleSelectConstant.DOCSTORE_TYPE_INSTANCE)) &&
224                         (format.equalsIgnoreCase(OleSelectConstant.DOCSTORE_FORMAT_OLEML))) {
225                     uuid = linkedInstanceDocument.getUuid();
226                 }
227             }
228             if (uuid != null && bibInfoBeanIterator.hasNext()) {
229                 BibInfoBean bibInfoBean = (BibInfoBean) bibInfoBeanIterator.next();
230                 if (bibInfoBean.getDocStoreIngestionId().equals(docStoreDocument.getId())) {
231                     bibInfoBean.setTitleId(uuid);
232                 }
233                 uuid = null;
234             }
235             if (LOG.isDebugEnabled())
236                 LOG.debug(docStoreDocument.getType() + " uuid---------->" + docStoreDocument.getUuid());
237         }
238         return bibInfoBeanList;
239     }
240 }