sd.code.stagent.fileservices.ReceiveFile.java Source code

Java tutorial

Introduction

Here is the source code for sd.code.stagent.fileservices.ReceiveFile.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sd.code.stagent.fileservices;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.DatatypeConverter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import sd.code.stagent.common.Web;
import sd.code.stagent.services.Shell;

/**
 *
 * @author motaz
 */
@WebServlet(name = "ReceiveFile", urlPatterns = { "/ReceiveFile" })
public class ReceiveFile extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String requestText;
            requestText = Web.readClient(request);
            try {

                JSONParser parser = new JSONParser();

                JSONObject obj = (JSONObject) parser.parse(requestText);

                String filename = obj.get("filename").toString();
                String dir = obj.get("dir").toString();

                boolean needConversion = filename.contains(".wav");
                String toDir = dir;

                if (needConversion) {
                    toDir = "/tmp/";
                }
                File file = new File(toDir + filename);
                FileOutputStream writer = new FileOutputStream(file);

                // Read parts

                JSONArray contents = (JSONArray) obj.get("content");
                for (int i = 0; i < contents.size(); i++) {
                    String part = contents.get(i).toString();
                    byte[] decoded = DatatypeConverter.parseBase64Binary(part);
                    writer.write(decoded);
                }

                writer.close();

                // Conversion
                if (needConversion) {
                    Shell.executeShell("sox " + toDir + filename + " " + dir
                            + filename.substring(0, filename.indexOf(".")) + ".gsm");
                }

                out.println("File " + filename + " has been written");

            } catch (Exception ex) {
                System.out.println(ex.toString());
                out.println("ErrirL : :" + ex.toString());
            }

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}