Package com.lbslocal.api.view

Source Code of com.lbslocal.api.view.ApiGZ

package com.lbslocal.api.view;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lbslocal.api.scriptcompact.ControlJavaScriptAPI;
import com.lbslocal.api.utils.ConfigurationManager;

/**
* Servlet implementation class ApiGZ
*/
public class ApiGZ extends HttpServlet {
  private static final long serialVersionUID = 1L;

  /**
   * @see HttpServlet#HttpServlet()
   */
  public ApiGZ() {
    super();
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String physicalPath = getServletContext().getRealPath("") + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + "com"
        + File.separator + "lbslocal" + File.separator + "api" + File.separator + "scripts";

    ConfigurationManager cm = ConfigurationManager.getInstance();
    com.lbslocal.master.common.ConfigurationManager cmMaster = com.lbslocal.master.common.ConfigurationManager.getInstance();

    ControlJavaScriptAPI memoryAPI = new ControlJavaScriptAPI(physicalPath, cmMaster.getAppSettings("PATH_FILE_API"));

    String AcceptEncoding, versionApi, engine, api, debug;
    boolean useGZip = true;

    engine = request.getParameter("engine") == null ? "" : request.getParameter("engine");
    versionApi = request.getParameter("v") == null ? "3" : request.getParameter("v");
    debug = request.getParameter("debug") == null ? "" : request.getParameter("debug");
    ServletOutputStream out = null;
    File file;
    RandomAccessFile raf = null;
    try {
      response.setContentType("application/x-javascript");

      AcceptEncoding = request.getHeader("Accept-Encoding");

      // *** Start by checking whether GZip is supported by client
      if (AcceptEncoding == null || AcceptEncoding.toLowerCase().indexOf("gzip") == -1)
        useGZip = false;

      api = "v" + versionApi + engine.toLowerCase();
     

      // é uma chamada de debug?
      if ("true".equals(cm.getAppSettings("debugJS")) || debug.equals("true")) {
        response.setHeader("Content-Encoding", "UTF-8");
        file = memoryAPI.getAPI(api, ControlJavaScriptAPI.Debug);
      } else {
        // é possivel usar o gzip?
        if (useGZip) {
          response.setHeader("Content-Encoding", "gzip");
          file = memoryAPI.getAPI(api, ControlJavaScriptAPI.GZip);
        } else {
          response.setHeader("Content-Encoding", "UTF-8");
          file = memoryAPI.getAPI(api, ControlJavaScriptAPI.Flat);
        }
      }
      raf = new RandomAccessFile(file, "r");
      response.setContentLength((int) raf.length());
      out = response.getOutputStream();
      byte[] loader = new byte[(int) raf.length()];
      while ((raf.read(loader)) > 0) {
        out.write(loader);
      }
     

    } catch (Exception e) {
      response.setContentType("application/x-javascript");

      StringBuilder eMsg = new StringBuilder(256).append("-- Gzip Maplink API Erro:").append("; QueryString: [").append(request.getQueryString()).append("]");
      response.getWriter().write("throw '" + eMsg + ":: Exception: " + e.getMessage() + "';");
    }finally{
     
      raf.close();
      out.flush();
      out.close();
    }
  }

}
TOP

Related Classes of com.lbslocal.api.view.ApiGZ

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.