Package com.itstherules.mediacentre

Source Code of com.itstherules.mediacentre.MediaCentreServlet

package com.itstherules.mediacentre;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

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

import com.itstherules.mediacentre.controller.Controllers;
import com.itstherules.mediacentre.controller.MediaCentreController;
import com.itstherules.mediacentre.freemarker.TemplateEngine;
import com.itstherules.mediacentre.model.MapMapConverter;
import com.itstherules.mediacentre.url.PathParser;
import com.itstherules.mediacentre.url.PathPart;

public class MediaCentreServlet extends HttpServlet {

  private static final long serialVersionUID = 4491148798003201722L;
 
  @Override
  @SuppressWarnings("unchecked")
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Map<String, Object> parameters = new MapMapConverter().convert(request.getParameterMap());
        Map<String, Object> parsed = new PathParser().parse(request.getRequestURI(), parameters);
        decorate(parsed);
        Controllers controllers = Controllers.valueOf((String) parsed.get(PathPart.CONTROLLER.name()));
      MediaCentreController controller = controllers.getController(new TemplateEngine(getServletContext()));
        controller.action((String) parsed.get(PathPart.ACTION.name()), parsed, response);
    } catch(IllegalArgumentException e){
      throw new ServletException(e);
    }
  }

  @SuppressWarnings("unchecked")
  private void decorate(Map<String, Object> parsed) {
    Enumeration<String> initParameterNames = getServletConfig().getInitParameterNames();
    while (initParameterNames.hasMoreElements()) {
      String name = initParameterNames.nextElement();
      parsed.put(name, getServletConfig().getInitParameter(name));
    }
    initParameterNames = getServletContext().getInitParameterNames();
    while (initParameterNames.hasMoreElements()) {
      String name = initParameterNames.nextElement();
      parsed.put(name, getServletContext().getInitParameter(name));
    }
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req, resp);
  }
 
}
TOP

Related Classes of com.itstherules.mediacentre.MediaCentreServlet

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.