1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select;
17
18 import org.apache.log4j.Logger;
19
20 import java.io.*;
21 import java.net.HttpURLConnection;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.net.UnknownHostException;
25
26 public class CitationParser {
27
28 private static final String CITATION_PARSER_URL = "http://freecite.library.brown.edu/citations/create";
29 private URL citationParserURL;
30 private Writer outputStreamWriter;
31 private Reader reader;
32 private Logger LOG = org.apache.log4j.Logger.getLogger(CitationParser.class);
33
34 public boolean isConnectionAlive() {
35 try {
36 URL citationParserURL = getURLObject();
37 HttpURLConnection urlConnection = (HttpURLConnection) citationParserURL.openConnection();
38 urlConnection.setConnectTimeout(1000);
39 if (urlConnection != null) {
40 urlConnection.getInputStream();
41 }
42 return urlConnection != null;
43
44 } catch (IOException e) {
45 return false;
46 }
47 }
48
49 public String parse(String object) {
50 String parsedCitation = "";
51
52 HttpURLConnection urlConnection = null;
53 try {
54 URL citationParserURL = getURLObject();
55 urlConnection = (HttpURLConnection) citationParserURL.openConnection();
56 setUrlConnectionProperties(urlConnection);
57 readCitationParmameters(object, urlConnection);
58 parsedCitation = readResultsFromCitationParser(urlConnection);
59 } catch (FileNotFoundException e) {
60 LOG.error("Invalid Citation Parser URL (" + citationParserURL + " ): " + e);
61 throw new RuntimeException("Invalid Citation Parser URL (" + citationParserURL + " ): " + e, e);
62 } catch (UnknownHostException e) {
63 LOG.error("Invalid Citation Parser URL (" + citationParserURL + " ): " + e);
64 throw new RuntimeException("Invalid Citation Parser URL (" + citationParserURL + " ): " + e, e);
65 } catch (IOException e) {
66 if (e.getMessage().indexOf("504") != -1) {
67 LOG.error("Connection Timeout: (" + citationParserURL + " ): " + e);
68 throw new RuntimeException("Connection Timeout: (" + citationParserURL + " ): " + e, e);
69 } else {
70 LOG.error("Internal Server Error or Service Unavailable: (" + citationParserURL + " ): " + e);
71 throw new RuntimeException("Internal Server Error or Service Unavailable (" + citationParserURL + " ): " + e, e);
72 }
73 } catch (Exception ex) {
74 LOG.error("Connection error:unable to connect " + citationParserURL + " : " + ex);
75 throw new RuntimeException("Connection error:unable to connect " + citationParserURL + " : " + ex, ex);
76 } finally {
77 if (urlConnection != null) {
78 urlConnection.disconnect();
79 }
80 }
81
82 return parsedCitation;
83 }
84
85 private URL getURLObject() throws MalformedURLException {
86 if (null == citationParserURL) {
87 citationParserURL = new URL(CITATION_PARSER_URL);
88 }
89 return citationParserURL;
90 }
91
92 public void setCitationParserURL(URL citationParserURL) {
93 this.citationParserURL = citationParserURL;
94 }
95
96 private String readResultsFromCitationParser(HttpURLConnection urlConnection) throws Exception {
97 InputStream inputStream = urlConnection.getInputStream();
98 StringWriter stringWriter = new StringWriter();
99 String parsedCitation = "";
100 try {
101 reader = getInputStreamReader(inputStream);
102 pipe(reader, stringWriter);
103 if (LOG.isDebugEnabled()) {
104 LOG.debug("response--------->" + stringWriter.toString());
105 }
106 parsedCitation = stringWriter.toString();
107 reader.close();
108 } catch (IOException e) {
109 throw new Exception("IOException while reading response", e);
110 } finally {
111 if (inputStream != null) {
112 inputStream.close();
113 }
114 }
115 return parsedCitation;
116 }
117
118 private Reader getInputStreamReader(InputStream inputStream) {
119
120 reader = new InputStreamReader(inputStream);
121
122 return reader;
123 }
124
125 private void readCitationParmameters(Object object, HttpURLConnection urlConnection) throws Exception {
126 Reader stringReader = new StringReader(object.toString());
127 OutputStream outputStream = urlConnection.getOutputStream();
128 try {
129 outputStreamWriter = getOutputStreamWriter(outputStream);
130 pipe(stringReader, outputStreamWriter);
131 outputStreamWriter.close();
132 } catch (IOException e) {
133 throw new Exception("IOException while posting stringReader", e);
134 } finally {
135 if (outputStream != null) {
136 outputStream.close();
137 }
138 }
139 }
140
141 public void setOutputStreamWriter(Writer outputStreamWriter) {
142 this.outputStreamWriter = outputStreamWriter;
143 }
144
145 public void setReader(Reader reader) {
146 this.reader = reader;
147 }
148
149 private Writer getOutputStreamWriter(OutputStream outputStream) throws UnsupportedEncodingException {
150
151 outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
152
153 return outputStreamWriter;
154 }
155
156 private void setUrlConnectionProperties(HttpURLConnection urlConnection) {
157 urlConnection.setDoOutput(true);
158 urlConnection.setDoInput(true);
159 urlConnection.setRequestProperty("Accept", "text/xml");
160
161 urlConnection.setConnectTimeout(5000);
162 }
163
164 private void pipe(Reader reader, Writer writer) throws IOException {
165 char[] buf = new char[1024];
166 int read = 0;
167 while ((read = reader.read(buf)) >= 0) {
168 writer.write(buf, 0, read);
169 }
170 writer.flush();
171 }
172
173 }