use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class GenericApi method dispatchApiResponse.
final void dispatchApiResponse(Object apiResponse, HttpServletRequest request, HttpServletResponse response) throws IOException {
if (apiResponse instanceof GenericFileDownloadResponse) {
GenericFileDownloadResponse gfdResponse = (GenericFileDownloadResponse) apiResponse;
String eTag = request.getHeader("If-None-Match");
if (eTag == null)
logger.log(Level.INFO, "No eTag found !");
if (eTag != null && eTag.equals(gfdResponse.getETag())) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
} else {
response.setContentType(gfdResponse.getMimeType());
response.setHeader("Cache-Control", "max-age=315360000");
response.setHeader("ETag", gfdResponse.getETag());
OutputStream out = response.getOutputStream();
out.write(gfdResponse.getData());
out.close();
}
} else if (apiResponse instanceof GenericResponse) {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
if (SystemProperty.STAGE.equals(SystemProperty.STAGE_GAMMA)) {
// response.setContentType( "application/json" );
response.addHeader("Access-Control-Allow-Origin", getAccessControlAllowOrigin());
}
PrintWriter writer = response.getWriter();
writer.println(new Gson().toJson(apiResponse));
writer.close();
} else if (apiResponse instanceof Throwable) {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
if (SystemProperty.STAGE.equals(SystemProperty.STAGE_GAMMA)) {
// response.setContentType( "application/json" );
response.addHeader("Access-Control-Allow-Origin", getAccessControlAllowOrigin());
}
PrintWriter writer = response.getWriter();
if (apiResponse instanceof InvalidArgumentException) {
logger.log(Level.INFO, ((Throwable) apiResponse).getMessage());
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
} else if (apiResponse instanceof InsufficientAccessException) {
logger.log(Level.INFO, ((Throwable) apiResponse).getMessage());
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
} else if (apiResponse instanceof UnexpectedServerException)
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
else
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
writer.println(((Throwable) apiResponse).getMessage());
writer.close();
}
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class EmailUtil method sendMail.
public static void sendMail(String senderName, String senderEmail, InternetAddress[] recipients, InternetAddress[] cc, String subject, String body) throws UnexpectedServerException {
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderEmail, senderName));
msg.addRecipients(Message.RecipientType.TO, recipients);
msg.addRecipients(Message.RecipientType.CC, cc);
// TODO: Uncomment when we migrate to other email service providers
// msg.addRecipient( Message.RecipientType.BCC, new InternetAddress( "mail-archive@pratilipi.com", "Mail Archive" ) );
msg.setSubject(subject);
msg.setContent(body, "text/html; charset=UTF-8");
logger.log(Level.INFO, "Sending mail");
Transport.send(msg);
} catch (UnsupportedEncodingException | MessagingException e) {
// logger.log( Level.SEVERE, "Failed to send mail to " + recipientEmail + ".", e );
throw new UnexpectedServerException();
}
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class HttpUtil method doPost.
public static String doPost(String targetURL, Map<String, String> paramsMap) throws UnexpectedServerException {
HttpURLConnection connection = null;
try {
// Forming content from request parameters
byte[] content = createQueryString(paramsMap).getBytes(Charset.forName("UTF-8"));
// Create connection
connection = (HttpURLConnection) new URL(targetURL).openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", content.length + "");
// 60 Seconds
connection.setConnectTimeout(60000);
// 60 Seconds
connection.setReadTimeout(60000);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
OutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(content);
outputStream.flush();
outputStream.close();
logger.log(Level.INFO, "Http POST Request: " + targetURL);
// Response
return _processPostResponse(connection);
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to execute Http Post call.", e);
throw new UnexpectedServerException();
} finally {
if (connection != null)
connection.disconnect();
}
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class HttpUtil method doPut.
public static String doPut(String targetURL, Map<String, String> headersMap, JsonObject jsonBody) throws UnexpectedServerException {
HttpURLConnection connection = null;
try {
// Forming request parameters
byte[] body = jsonBody.toString().getBytes(Charset.forName("UTF-8"));
// Create connection
URL url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", body.length + "");
for (Entry<String, String> entry : headersMap.entrySet()) connection.setRequestProperty(entry.getKey(), entry.getValue());
// 60 Seconds
connection.setConnectTimeout(60000);
// 60 Seconds
connection.setReadTimeout(60000);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
OutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(body);
outputStream.flush();
outputStream.close();
// Response
logger.log(Level.INFO, "Http POST Request: " + targetURL);
return _processPostResponse(connection);
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to execute Http Post call.", e);
throw new UnexpectedServerException();
} finally {
if (connection != null)
connection.disconnect();
}
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class HttpUtil method doPost.
public static String doPost(String targetURL, Map<String, String> headersMap, JsonObject jsonBody) throws UnexpectedServerException {
HttpURLConnection connection = null;
try {
// Forming request parameters
byte[] body = jsonBody.toString().getBytes(Charset.forName("UTF-8"));
// Create connection
URL url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", body.length + "");
for (Entry<String, String> entry : headersMap.entrySet()) connection.setRequestProperty(entry.getKey(), entry.getValue());
// 60 Seconds
connection.setConnectTimeout(60000);
// 60 Seconds
connection.setReadTimeout(60000);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
OutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(body);
outputStream.flush();
outputStream.close();
// Response
logger.log(Level.INFO, "Http POST Request: " + targetURL);
return _processPostResponse(connection);
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to execute Http Post call.", e);
throw new UnexpectedServerException();
} finally {
if (connection != null)
connection.disconnect();
}
}
Aggregations