001    package com.khubla.pragmatach.framework.servlet;
002    
003    import java.io.IOException;
004    import java.util.Map;
005    import java.util.Map.Entry;
006    
007    import javax.servlet.ServletConfig;
008    import javax.servlet.ServletException;
009    import javax.servlet.http.HttpServlet;
010    import javax.servlet.http.HttpServletRequest;
011    import javax.servlet.http.HttpServletResponse;
012    
013    import com.khubla.pragmatach.framework.annotation.Route;
014    import com.khubla.pragmatach.framework.api.Request;
015    import com.khubla.pragmatach.framework.api.Response;
016    import com.khubla.pragmatach.framework.jmx.impl.PerformanceStatistics;
017    import com.khubla.pragmatach.framework.router.Router;
018    
019    /**
020     * @author tome
021     */
022    public class PragmatachServlet extends HttpServlet {
023            /**
024        * 
025        */
026            private static final long serialVersionUID = 1L;
027            /**
028             * render time header
029             */
030            private static final String RENDERTIME_HEADER = "X-Pragmatach-RenderTime";
031            /**
032             * performance stats.
033             */
034            private static final PerformanceStatistics performanceStatistics = new PerformanceStatistics();
035    
036            public synchronized static PerformanceStatistics getPerformancestatistics() {
037                    return performanceStatistics;
038            }
039    
040            /**
041             * add custom response headers
042             */
043            private void addCustomResponseHeaders(Request request,
044                            HttpServletResponse httpServletResponse) {
045                    /*
046                     * add the render time
047                     */
048                    final long rendertime = System.currentTimeMillis()
049                                    - request.getCreationTime();
050                    /*
051                     * add
052                     */
053                    httpServletResponse.addHeader(RENDERTIME_HEADER,
054                                    Long.toString(rendertime));
055                    /*
056                     * record it too
057                     */
058                    getPerformancestatistics().setLastRenderTime(rendertime);
059                    /*
060                     * record a request
061                     */
062                    getPerformancestatistics().incrementTotalRequests();
063            }
064    
065            @Override
066            protected void doGet(HttpServletRequest httpServletRequest,
067                            HttpServletResponse httpServletResponse) throws ServletException,
068                            IOException {
069                    try {
070                            final Router requestRouter = new Router();
071                            final Request request = new Request(httpServletRequest,
072                                            httpServletResponse, Route.HttpMethod.get,
073                                            getServletConfig());
074                            final Response response = requestRouter.route(request);
075                            addCustomResponseHeaders(request, httpServletResponse);
076                            processResponse(response, httpServletResponse);
077                    } catch (final Exception e) {
078                            throw new ServletException("Exception in doGet", e);
079                    }
080            }
081    
082            @Override
083            protected void doPost(HttpServletRequest httpServletRequest,
084                            HttpServletResponse httpServletResponse) throws ServletException,
085                            IOException {
086                    try {
087                            final Router requestRouter = new Router();
088                            final Request request = new Request(httpServletRequest,
089                                            httpServletResponse, Route.HttpMethod.post,
090                                            getServletConfig());
091                            final Response response = requestRouter.route(request);
092                            addCustomResponseHeaders(request, httpServletResponse);
093                            processResponse(response, httpServletResponse);
094                    } catch (final Exception e) {
095                            throw new ServletException("Exception in doGet", e);
096                    }
097            }
098    
099            /**
100             * init
101             */
102            @Override
103            public void init(ServletConfig servletConfig) throws ServletException {
104                    try {
105                            super.init(servletConfig);
106                    } catch (final Exception e) {
107                            throw new ServletException("Exception in init", e);
108                    }
109            }
110    
111            /**
112             * process the response
113             */
114            private void processResponse(Response response,
115                            HttpServletResponse httpServletResponse) throws ServletException {
116                    try {
117                            if (null != response) {
118                                    final Map<String, String> headers = response.getHeaders();
119                                    if (null != headers) {
120                                            for (final Entry<String, String> entry : headers.entrySet()) {
121                                                    httpServletResponse.setHeader(entry.getKey(),
122                                                                    entry.getValue());
123                                            }
124                                    }
125                                    response.render(httpServletResponse);
126                                    final String contentType = response.getContentType();
127                                    if (null != contentType) {
128                                            httpServletResponse.setContentType(contentType);
129                                    }
130                                    httpServletResponse.setStatus(response.getHTTPCode());
131                            }
132                    } catch (final Exception e) {
133                            throw new ServletException("Exception in processResponse", e);
134                    }
135            }
136    }