4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SECAPIConn.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Jaroslav Dytrych, idytrych@fit.vutbr.cz
4  * File: SECAPIConn.java
5  * Description: Class implementing functions for communication with remote SEC API
6  */
7 
8 /**
9  * @file SECAPIConn.java
10  *
11  * @brief Class implementing functions for communication with remote SEC API
12  */
13 
14 package cz.vutbr.fit.knot.annotations.modules.suggestionManager;
15 
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.io.OutputStreamWriter;
25 import java.net.HttpURLConnection;
26 import java.net.MalformedURLException;
27 import java.net.ProtocolException;
28 import java.net.URL;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 
32 /**
33  * Class implementing functions for communication with remote SEC API
34  *
35  * @brief Class implementing functions for communication with remote SEC API
36  */
37 public class SECAPIConn extends Thread {
38  private URL secServ;
39  // connection for a requests
40  private HttpURLConnection conn;
41  // write stream to the SEC API server
42  private OutputStreamWriter outW;
43  // reader stream from the SEC API server
44  private BufferedReader inR;
45 
46 
47  /**
48  * Constructor.
49  * Initializes connection to the remote SEC API.
50  * Creates objects for communication.
51  */
52  public SECAPIConn() {
53  try {
54  secServ = new URL(AppBean.getSecApiServerUri());
55  }
56  catch (MalformedURLException e) {
58  String msg = "Unable to create connection from provided URI.";
59  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
60  }
61  }
62  catch (IOException e){
64  String msg = "Error opening a connection to provided URI.";
65  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
66  }
67  }
68  }
69 
70  /**
71  * Constructor.
72  * Initializes connection to the SEC API.
73  * Creates objects for communication.
74  *
75  * @param secUri Address of the remote SEC API server
76  */
77  public SECAPIConn(String secUri) {
78  try {
79  secServ = new URL(secUri);
80  }
81  catch (MalformedURLException e) {
83  String msg = "Malformed SEC Store API server URI.";
84  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
85  }
86  }
87  catch(IOException e){
89  String msg = "Error opening a connection to provided SEC Store API server URI.";
90  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
91  }
92  }
93  }
94 
95  /**
96  * Method sends data to the SEC API server.
97  *
98  * @param requestData Data containing request to SEC API server
99  * @param requestInfo Info about client request
100  * @return True if operation was successful, false otherwise
101  */
102  public String sendReqToSECAPI(String requestData, RequestInfo requestInfo) {
103  StringBuilder ret = new StringBuilder("");
104  try {
105  conn = (HttpURLConnection) secServ.openConnection();
106  }
107  catch (IOException e) {
108  if(requestInfo != null){
109  int langNum = requestInfo.getSession().getLanguageNum();
110  int lod = requestInfo.getSession().getProtocolLOD();
111  requestInfo.addError(lod, langNum, Localisation.ERROR_60_SEC_NOT_AVAILABLE);
112  }
114  String msg = "Unable to open connection to the SEC API server.";
115  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
116  }
117  return null;
118  }
119  if (conn == null) {
120  if(requestInfo != null){
121  int langNum = requestInfo.getSession().getLanguageNum();
122  int lod = requestInfo.getSession().getProtocolLOD();
123  requestInfo.addError(lod, langNum, Localisation.ERROR_60_SEC_NOT_AVAILABLE);
124  }
126  String msg = "Unable to open connection to the SEC API server.";
127  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
128  }
129  return null;
130  }
131 
132  // set request properties
133  try {
134  conn.setRequestMethod("POST");
135  conn.setDoOutput(true);
136  conn.setDoInput(true);
137  conn.addRequestProperty("Content-Length", Integer.toString(requestData.length()));
138  }
139  catch (ProtocolException e) {
140  if(requestInfo != null){
141  int langNum = requestInfo.getSession().getLanguageNum();
142  int lod = requestInfo.getSession().getProtocolLOD();
143  requestInfo.addError(lod, langNum, Localisation.ERROR_60_SEC_NOT_AVAILABLE);
144  }
146  String msg = "Unable to set protocol request method for SEC API connection.";
147  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
148  }
149  return null;
150  }
151 
152  // send request data via HTTP to the SEC API server
153  try {
154  outW = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
155  outW.write(requestData);
156  outW.close();
157  }
158  catch (IOException e) {
159  if(requestInfo != null){
160  int langNum = requestInfo.getSession().getLanguageNum();
161  int lod = requestInfo.getSession().getProtocolLOD();
162  requestInfo.addError(lod, langNum, Localisation.ERROR_60_SEC_NOT_AVAILABLE);
163  }
165  String msg = "Unable to get output stream for SEC API connection.";
166  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
167  }
168  return null;
169  }
170 
171  // read SEC API server's response data
172  InputStream is;
173  try {
174  is = conn.getInputStream();
175  }
176  catch (Exception e) {
177  if(requestInfo != null){
178  int langNum = requestInfo.getSession().getLanguageNum();
179  int lod = requestInfo.getSession().getProtocolLOD();
180  requestInfo.addError(lod, langNum, Localisation.ERROR_60_SEC_NOT_AVAILABLE);
181  }
183  String msg = "Unable to get input stream for SEC API connection.";
184  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
185  }
186  return null;
187  }
188  InputStreamReader isr = new InputStreamReader(is);
189  inR = new BufferedReader(isr);
190  String inputLine;
191  try {
192  while((inputLine = inR.readLine()) != null) {
193  ret.append(inputLine);
194  }
195  inR.close();
196  }
197  catch (IOException e) {
198  if(requestInfo != null){
199  int langNum = requestInfo.getSession().getLanguageNum();
200  int lod = requestInfo.getSession().getProtocolLOD();
201  requestInfo.addError(lod, langNum, Localisation.ERROR_60_SEC_NOT_AVAILABLE);
202  }
204  String msg = "Unable to read from input stream connected to SEC API server.";
205  Logger.getLogger(SECAPIConn.class.getName()).log(Level.SEVERE, msg);
206  }
207  return null;
208  }
209 
210  return ret.toString();
211  } // sendReqToSECAPI()
212 } // class SECAPIConn
String sendReqToSECAPI(String requestData, RequestInfo requestInfo)
Singleton for storing global variables.
Definition: AppBean.java:47
Class implementing functions for communication with remote SEC API.
Definition: SECAPIConn.java:37
Processed informations about client request.
Class responsible for localised strings.