Search in sources :

Example 16 with UnexpectedServerException

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();
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) GenericResponse(com.pratilipi.api.shared.GenericResponse) OutputStream(java.io.OutputStream) GenericFileDownloadResponse(com.pratilipi.api.shared.GenericFileDownloadResponse) Gson(com.google.gson.Gson) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PrintWriter(java.io.PrintWriter)

Example 17 with UnexpectedServerException

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();
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 18 with 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();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) DataOutputStream(java.io.DataOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL)

Example 19 with UnexpectedServerException

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();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) DataOutputStream(java.io.DataOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL)

Example 20 with 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> 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();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) DataOutputStream(java.io.DataOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)46 IOException (java.io.IOException)19 JsonObject (com.google.gson.JsonObject)12 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)12 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 HashMap (java.util.HashMap)12 Gson (com.google.gson.Gson)10 DataAccessor (com.pratilipi.data.DataAccessor)10 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)6 BlobEntry (com.pratilipi.data.type.BlobEntry)6 Date (java.util.Date)6 File (java.io.File)5 JsonElement (com.google.gson.JsonElement)4 Get (com.pratilipi.api.annotation.Get)4 Post (com.pratilipi.api.annotation.Post)4 GenericResponse (com.pratilipi.api.shared.GenericResponse)4 OutputStream (java.io.OutputStream)4 URL (java.net.URL)4 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)3 Page (com.pratilipi.data.type.Page)3