// Path: flexiblepower.ui/src/org/flexiblepower/runtime/ui/server/HttpUtils.java
// public final class HttpUtils {
//     private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
// 
//     private static final Map<String, String> CONTEINT_TYPES = new HashMap<String, String>();
// 
//     static {
//         add("text/html", "html", "htm");
//         add("text/css", "css");
//         add("text/xml", "xml");
//         add("application/javascript", "js");
//         add("image/png", "png");
//         add("image/jpeg", "jpg", "jpeg");
//         add("image/gif", "gif");
//         add("image/svg", "svg");
//     }
// 
//     private HttpUtils() {
//     }
// 
//     private static void add(String contentType, String... extensions) {
//         logger.trace("Entering add, contentType = {}, extensions = {}", contentType, extensions);
//         for (String ext : extensions) {
//             CONTEINT_TYPES.put(ext, contentType);
//         }
//         logger.trace("Leaving add");
//     }
// 
//     public static String getContentType(String filename) {
//         logger.trace("Entering getContentType, filename = {}", filename);
//         int ix = filename.lastIndexOf('.');
//         if (ix > 0) {
//             String ext = filename.substring(ix + 1).toLowerCase();
//             if (CONTEINT_TYPES.containsKey(ext)) {
//                 return CONTEINT_TYPES.get(ext);
//             }
//         }
//         String result = URLConnection.guessContentTypeFromName(filename);
//         logger.trace("Leaving getContentType, result = {}", result);
//         return result;
//     }
// 
//     public static void setNoCaching(HttpServletResponse resp) {
//         logger.trace("Entering setNoCaching, resp = {}", resp.getClass());
//         resp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
//         resp.setHeader("Pragma", "no-cache"); // HTTP 1.0
//         resp.setDateHeader("Expires", 0);
//         logger.trace("Leaving setNoCaching");
//     }
// 
//     public static void
//             writeFile(URL url, long expirationTime, String name, HttpServletResponse resp, Locale locale) throws IOException {
//         logger.trace("Entering writeFile, url = {}, expirationTime = {}, name = {}, locale = {}",
//                      url,
//                      expirationTime,
//                      name,
//                      locale);
//         if (url == null) {
//             resp.sendError(404);
//         } else {
//             if (expirationTime <= 0) {
//                 setNoCaching(resp);
//             } else {
//                 resp.setDateHeader("Expires", System.currentTimeMillis() + expirationTime);
//             }
// 
//             resp.setContentType(getContentType(name));
//             if (name.endsWith(".html") || name.endsWith(".css") || name.endsWith(".js")) {
//                 resp.setCharacterEncoding("UTF-8");
//             }
//             write(url.openStream(), resp.getOutputStream());
//         }
//         logger.trace("Leaving writeFile");
//     }
// 
//     public static void write(InputStream input, OutputStream output) throws IOException {
//         logger.trace("Entering write, input = {}, output = {}", input, output);
//         byte[] buffer = new byte[4096];
//         int read = 0;
//         try {
//             while ((read = input.read(buffer)) >= 0) {
//                 output.write(buffer, 0, read);
//             }
//         } finally {
//             input.close();
//         }
//         logger.trace("Leaving write", input, output);
//     }
// 
//     public static String readData(InputStream input) throws IOException {
//         logger.trace("Entering readData, input = {}", input);
//         try {
//             Reader reader = new InputStreamReader(input);
//             StringBuilder sb = new StringBuilder();
//             char[] buffer = new char[1024];
//             int read = 0;
//             while ((read = reader.read(buffer)) >= 0) {
//                 sb.append(buffer, 0, read);
//             }
//             logger.trace("Levaing readData, result = (String of {} characters)", sb.length());
//             return sb.toString();
//         } finally {
//             input.close();
//         }
//     }
// }


import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.flexiblepower.runtime.ui.server.HttpUtils;
import org.flexiblepower.runtime.ui.server.widgets.WidgetRegistration;
import org.flexiblepower.ui.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package org.flexiblepower.runtime.ui.server.pages;




public class DashboardWidgetServlet extends HttpServlet {
    private static final long serialVersionUID = -2367978642451243228L;
    private static final Logger logger = LoggerFactory.getLogger(DashboardWidgetServlet.class);

    private final WidgetRegistration registration;
    private final long expirationTime;

    public DashboardWidgetServlet(WidgetRegistration registration, long expirationTime) {
        logger.trace("Entering constructor, registration = {}, expirationTime = {}", registration, expirationTime);
        this.registration = registration;
        this.expirationTime = expirationTime;
        logger.trace("Leaving constructor");
    }

    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        logger.trace("Entering service, req.pathInfo = {}", req.getPathInfo());
        if (req.getPathInfo() == null || req.getPathInfo().isEmpty()) {
            String url = "/widget/" + registration.getId() + "/index.html";
            resp.sendRedirect(url);
            logger.trace("Leaving service, redirected to {}", url);
            return;
        }
        String path = req.getPathInfo().substring(1);

        URL url = registration.getResource(path, req.getLocale());

        if (url == null) {
            logger.debug("No such file, calling method {}", path);
            User user = (User) req.getAttribute("user");
            try {
// Path: flexiblepower.ui/src/org/flexiblepower/runtime/ui/server/HttpUtils.java
// public final class HttpUtils {
//     private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
// 
//     private static final Map<String, String> CONTEINT_TYPES = new HashMap<String, String>();
// 
//     static {
//         add("text/html", "html", "htm");
//         add("text/css", "css");
//         add("text/xml", "xml");
//         add("application/javascript", "js");
//         add("image/png", "png");
//         add("image/jpeg", "jpg", "jpeg");
//         add("image/gif", "gif");
//         add("image/svg", "svg");
//     }
// 
//     private HttpUtils() {
//     }
// 
//     private static void add(String contentType, String... extensions) {
//         logger.trace("Entering add, contentType = {}, extensions = {}", contentType, extensions);
//         for (String ext : extensions) {
//             CONTEINT_TYPES.put(ext, contentType);
//         }
//         logger.trace("Leaving add");
//     }
// 
//     public static String getContentType(String filename) {
//         logger.trace("Entering getContentType, filename = {}", filename);
//         int ix = filename.lastIndexOf('.');
//         if (ix > 0) {
//             String ext = filename.substring(ix + 1).toLowerCase();
//             if (CONTEINT_TYPES.containsKey(ext)) {
//                 return CONTEINT_TYPES.get(ext);
//             }
//         }
//         String result = URLConnection.guessContentTypeFromName(filename);
//         logger.trace("Leaving getContentType, result = {}", result);
//         return result;
//     }
// 
//     public static void setNoCaching(HttpServletResponse resp) {
//         logger.trace("Entering setNoCaching, resp = {}", resp.getClass());
//         resp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
//         resp.setHeader("Pragma", "no-cache"); // HTTP 1.0
//         resp.setDateHeader("Expires", 0);
//         logger.trace("Leaving setNoCaching");
//     }
// 
//     public static void
//             writeFile(URL url, long expirationTime, String name, HttpServletResponse resp, Locale locale) throws IOException {
//         logger.trace("Entering writeFile, url = {}, expirationTime = {}, name = {}, locale = {}",
//                      url,
//                      expirationTime,
//                      name,
//                      locale);
//         if (url == null) {
//             resp.sendError(404);
//         } else {
//             if (expirationTime <= 0) {
//                 setNoCaching(resp);
//             } else {
//                 resp.setDateHeader("Expires", System.currentTimeMillis() + expirationTime);
//             }
// 
//             resp.setContentType(getContentType(name));
//             if (name.endsWith(".html") || name.endsWith(".css") || name.endsWith(".js")) {
//                 resp.setCharacterEncoding("UTF-8");
//             }
//             write(url.openStream(), resp.getOutputStream());
//         }
//         logger.trace("Leaving writeFile");
//     }
// 
//     public static void write(InputStream input, OutputStream output) throws IOException {
//         logger.trace("Entering write, input = {}, output = {}", input, output);
//         byte[] buffer = new byte[4096];
//         int read = 0;
//         try {
//             while ((read = input.read(buffer)) >= 0) {
//                 output.write(buffer, 0, read);
//             }
//         } finally {
//             input.close();
//         }
//         logger.trace("Leaving write", input, output);
//     }
// 
//     public static String readData(InputStream input) throws IOException {
//         logger.trace("Entering readData, input = {}", input);
//         try {
//             Reader reader = new InputStreamReader(input);
//             StringBuilder sb = new StringBuilder();
//             char[] buffer = new char[1024];
//             int read = 0;
//             while ((read = reader.read(buffer)) >= 0) {
//                 sb.append(buffer, 0, read);
//             }
//             logger.trace("Levaing readData, result = (String of {} characters)", sb.length());
//             return sb.toString();
//         } finally {
//             input.close();
//         }
//     }
// }


// Path: flexiblepower.ui/src/org/flexiblepower/runtime/ui/server/pages/DashboardWidgetServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.flexiblepower.runtime.ui.server.HttpUtils;
import org.flexiblepower.runtime.ui.server.widgets.WidgetRegistration;
import org.flexiblepower.ui.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

package org.flexiblepower.runtime.ui.server.pages;




public class DashboardWidgetServlet extends HttpServlet {
    private static final long serialVersionUID = -2367978642451243228L;
    private static final Logger logger = LoggerFactory.getLogger(DashboardWidgetServlet.class);

    private final WidgetRegistration registration;
    private final long expirationTime;

    public DashboardWidgetServlet(WidgetRegistration registration, long expirationTime) {
        logger.trace("Entering constructor, registration = {}, expirationTime = {}", registration, expirationTime);
        this.registration = registration;
        this.expirationTime = expirationTime;
        logger.trace("Leaving constructor");
    }

    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        logger.trace("Entering service, req.pathInfo = {}", req.getPathInfo());
        if (req.getPathInfo() == null || req.getPathInfo().isEmpty()) {
            String url = "/widget/" + registration.getId() + "/index.html";
            resp.sendRedirect(url);
            logger.trace("Leaving service, redirected to {}", url);
            return;
        }
        String path = req.getPathInfo().substring(1);

        URL url = registration.getResource(path, req.getLocale());

        if (url == null) {
            logger.debug("No such file, calling method {}", path);
            User user = (User) req.getAttribute("user");
            try {
                String input = HttpUtils.readData(req.getInputStream());