Example usage for javax.servlet ServletRequest getProtocol

List of usage examples for javax.servlet ServletRequest getProtocol

Introduction

In this page you can find the example usage for javax.servlet ServletRequest getProtocol.

Prototype

public String getProtocol();

Source Link

Document

Returns the name and version of the protocol the request uses in the form <i>protocol/majorVersion.minorVersion</i>, for example, HTTP/1.1.

Usage

From source file:com.evon.injectTemplate.InjectTemplateFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    StringWriter writer = new StringWriter();
    BufferedResponseWrapper wrapper = new BufferedResponseWrapper((HttpServletResponse) response, writer);
    chain.doFilter(request, wrapper);/*  w w w.j  av a  2 s.c o  m*/

    String contentType = null;
    String uri = httpRequest.getRequestURI();

    contentType = httpResponse.getContentType();

    if (contentTypes.containsKey(uri)) {
        contentType = contentTypes.get(uri);
    } else if (contentType != null) {
        contentTypes.put(uri, contentType);
    }
    contentType = (contentType == null) ? "none" : contentType;

    String out = writer.getBuffer().toString();

    boolean requestedURIIsTheTemplate = false;

    requestedURIIsTheTemplate = InjectUtils.isTemplate(uri);

    boolean contentTypeIsText = !wrapper.isBinary() && contentType != null && !contentType.equals("none");

    if (requestedURIIsTheTemplate || !contentTypeIsText) {
        if (contentTypeIsText) {
            response.getWriter().print(out);
        }
        return;
    }

    if (!templateLodaded && !templateLodadedStarted) {
        loadTemplates(httpRequest);
    }

    TemplateBean template = getTemplatePathByURI(uri);

    if (template == null) {
        response.getWriter().print(out);
        return;
    }

    String key = null;

    if (template.cache.equals("SESSION")) {
        String cookiesNames = getCookieHashs(httpRequest);
        key = template.path + cookiesNames;
    } else if (template.cache.equals("ON"))

    {
        key = template.path;

    }

    if (!templates.containsKey("/" + template.path)) {
        throw new ServletException("Template [" + template.path + "] not founded");
    }

    boolean needRefresh = template.refreshInSeconds > 0
            && ((System.currentTimeMillis() - template.lastUpdate) / 1000) > template.refreshInSeconds;

    boolean replaceTemplate = template.cache.equals("OFF") || !htmlContents.containsKey(key) || needRefresh;

    /*   pra investigar erro de nulo,    
          boolean notExist = key==null || !htmlContents.containsKey(key);
     * if(!notExist)
          {
             notExist=!notExist;
             //throw new ServletException("content not exist");
          }
       */

    if (replaceTemplate) {
        if (needRefresh) {
            template.lastUpdate = System.currentTimeMillis();
        }

        try {
            loadContentTemplate(template, request.getServerName(), request.getServerPort(),
                    request.getProtocol().contains("HTTPS"), httpRequest);
        } catch (Exception e) {
            throw new ServletException(e.getMessage(), e);
        }
    }

    HTMLInfoBean templateHTML = templates.get("/" + template.path);

    String contentTemplate = htmlContents.get(key).getContent();
    IDocument docOut = HTMLParser.parse(out);

    for (String selector : templateHTML.getSelectors()) {
        IElements elements = docOut.select(selector);
        if (elements.size() == 0) {
            System.out.println("WARNING: Selector [" + selector + "] in template [" + templateHTML.getUri()
                    + "] not founded in [" + httpRequest.getRequestURI() + "]");
            continue;
        }
        if (elements.size() != 1) {
            System.out.println(
                    "WARNING: Selector get many elements. Choosed the first to URI: " + templateHTML.getUri());
        }
        IElement element = elements.get(0);
        String innerHTML = element.html();

        contentTemplate = contentTemplate.replace("<INJECT selector='" + selector + "'/>", innerHTML);
    }

    response.getWriter().print(contentTemplate);
}