1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
53 try {
54
55
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
64
65
66
67
68
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public String getDataFromResponseXMLForDocStore(String response, HashMap<String, String> dataMap) throws Exception {
140 StringBuilder uuid = new StringBuilder();
141
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
156
157
158
159
160
161
162
163
164
165
166
167
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
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
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 }