package com.github.therapi.jsonrpc.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * When deploying to servlet containers that do not support Servlet API 3.0
 * (Google App Engine, I'm looking at you) this servlet serves up the resources
 * that would normally be loaded automatically from the META-INF/resources
 * directory inside the Therapi JAR.
 * <p>
 * Add the following snippet to your web.xml and you should be good to go.
 * <pre>
 * &lt;servlet>
 *     &lt;servlet-name>MetaInfResourceExporterServlet&lt;/servlet-name>
 *     &lt;servlet-class>com.github.therapi.jsonrpc.web.MetaInfResourceExporterServlet&lt;/servlet-class>
 *     &lt;load-on-startup>0&lt;/load-on-startup>
 * &lt;/servlet>
 * &lt;servlet-mapping>
 *     &lt;servlet-name>MetaInfResourceExporterServlet&lt;/servlet-name>
 *     &lt;url-pattern>/therapi/*&lt;/url-pattern>
 * &lt;/servlet-mapping>
 * </pre>
 */
public class MetaInfResourceExporterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();

        String resourceName = "/META-INF/resources/therapi" + pathInfo;

        try (InputStream is = getClass().getResourceAsStream(resourceName)) {
            if (is == null) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, resourceName);
                return;
            }

            String contentType = null;
            if (resourceName.endsWith(".js")) {
                contentType = "text/javascript";
            } else if (resourceName.endsWith(".css")) {
                contentType = "text/css";
            } else if (resourceName.endsWith(".png")) {
                contentType = "image/png";
            }

            if (contentType != null) {
                response.setContentType(contentType);
            }

            try (OutputStream os = response.getOutputStream()) {
                byte[] buffer = new byte[4096];
                int len;
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                }
            }
        }
    }
}
@@ -664,25 +664,28 @@
  &lt;servlet
-%3E
+&gt;
 %0A *     &lt;
@@ -688,33 +688,36 @@
 &lt;servlet-name
-%3E
+&gt;
 MetaInfResourceE
@@ -739,33 +739,36 @@
 lt;/servlet-name
-%3E
+&gt;
 %0A *     &lt;serv
@@ -776,17 +776,20 @@
 et-class
-%3E
+&gt;
 com.gith
@@ -859,17 +859,20 @@
 et-class
-%3E
+&gt;
 %0A *     
@@ -890,17 +890,20 @@
 -startup
-%3E
+&gt;
 0&lt;/lo
@@ -915,17 +915,20 @@
 -startup
-%3E
+&gt;
 %0A * &lt;
@@ -935,17 +935,20 @@
 /servlet
-%3E
+&gt;
 %0A * &lt;
@@ -958,25 +958,28 @@
 vlet-mapping
-%3E
+&gt;
 %0A *     &lt;
@@ -990,17 +990,20 @@
 let-name
-%3E
+&gt;
 MetaInfR
@@ -1041,17 +1041,20 @@
 let-name
-%3E
+&gt;
 %0A *     
@@ -1068,17 +1068,20 @@
 -pattern
-%3E
+&gt;
 /therapi
@@ -1098,17 +1098,20 @@
 -pattern
-%3E
+&gt;
 %0A * &lt;
@@ -1126,17 +1126,20 @@
 -mapping
-%3E
+&gt;
 %0A * %3C/pr

Fix Javadoc errors