Example usage for javax.servlet.http HttpServlet HttpServlet

List of usage examples for javax.servlet.http HttpServlet HttpServlet

Introduction

In this page you can find the example usage for javax.servlet.http HttpServlet HttpServlet.

Prototype


public HttpServlet() 

Source Link

Document

Does nothing, because this is an abstract class.

Usage

From source file:fr.xebia.servlet.filter.ExpiresFilterTest.java

@Test
public void testSkipBecauseCacheControlMaxAgeIsDefined() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//from   w  w  w.j  a v a  2 s.c o  m
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.addHeader("Cache-Control", "private, max-age=232");
            response.getWriter().print("Hello world");
        }
    };

    int expectedMaxAgeInSeconds = 232;
    validate(servlet, expectedMaxAgeInSeconds);
}

From source file:org.apache.sling.etcd.client.impl.EtcdClientImplTest.java

@Test
public void testMultiValueHeader() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        @Override//from  w w w.  j  av a2s.c  o m
        protected void service(HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {
            res.addHeader("headerName", "value1");
            res.addHeader("headerName", "value2");
            res.getWriter().write(IOUtils.toString(getClass().getResourceAsStream("/action-1.json")));
        }
    };
    server1 = startServer(servlet, "/v2/keys/test");
    buildEtcdClient(serverPort(server1));
    KeyResponse response = etcdClient.putKey("/test", "test-data", EtcdParams.builder().ttl(10).build());
    Assert.assertNotNull(response.headers());
    Assert.assertNotNull(response.header("headerName"));
    List<String> headers = response.header("headerName");
    Assert.assertEquals(2, headers.size());
    Assert.assertEquals("value1", headers.get(0));
    Assert.assertEquals("value2", headers.get(1));
    Assert.assertEquals("value1", response.headerFirst("headerName"));
}

From source file:fr.xebia.servlet.filter.ExpiresFilterTest.java

@Test
public void testExcludedResponseStatusCode() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override/*from  ww w .j av  a 2s . co m*/
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.addHeader("ETag", "W/\"1934-1269208821000\"");
            response.addDateHeader("Date", System.currentTimeMillis());
        }
    };

    validate(servlet, null, HttpServletResponse.SC_NOT_MODIFIED);
}

From source file:fr.xebia.servlet.filter.ExpiresFilterTest.java

@Test
public void testNullContentType() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override/*from www.  jav a  2 s.c  o m*/
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType(null);
        }
    };

    int expectedMaxAgeInSeconds = 1 * 60;
    validate(servlet, expectedMaxAgeInSeconds);
}

From source file:org.apache.sling.etcd.client.impl.EtcdClientImplTest.java

@Test
public void testPutRequestFormat() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        @Override/*w w w . j  a va  2s  .c om*/
        protected void service(HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {
            ContentType contentType = ContentType.parse(req.getContentType());
            if (!contentType.getMimeType().equals(EtcdClientImpl.FORM_URLENCODED.getMimeType())) {
                throw new IllegalArgumentException("wrong mime type");
            }
            if (!contentType.getCharset().equals(EtcdClientImpl.FORM_URLENCODED.getCharset())) {
                throw new IllegalArgumentException("wrong content charset");
            }
            String value = req.getParameter("value");
            if (value == null) {
                throw new IllegalArgumentException("missing value parameter");
            }
            String ttl = req.getParameter("ttl");
            if (!"10".equals(ttl)) {
                throw new IllegalArgumentException("missing ttl parameter");
            }
            res.setStatus(201);
            res.getWriter().write(IOUtils.toString(getClass().getResourceAsStream("/action-3.json")));
        }
    };
    server1 = startServer(servlet, "/v2/keys/post/test");
    buildEtcdClient(serverPort(server1));
    KeyResponse response = etcdClient.putKey("/post/test", "test-data", Collections.singletonMap("ttl", "10"));
    Assert.assertNotNull(response);
    Assert.assertTrue(response.isAction());
}

From source file:org.apache.sling.discovery.etcd.EtcdServiceTest.java

@Test
public void testGetProperties() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        @Override/*from  ww w  . j ava  2s.  c  o m*/
        protected void service(HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {
            if (!"GET".equals(req.getMethod())) {
                throw new IllegalArgumentException("get properties requires GET");
            }
            String recursive = req.getParameter("recursive");
            if (recursive == null || "false".equals(recursive)) {
                throw new IllegalArgumentException("recursive must be true");
            }
            res.setStatus(200);
            res.getWriter().write(IOUtils.toString(getClass().getResourceAsStream("/get-properties.json")));
        }
    };
    server = startServer(servlet, "/v2/keys/discovery/properties/sling-id");
    EtcdService etcdService = buildEtcdService(serverPort(server));
    Map<String, String> properties = etcdService.getProperties("sling-id");
    Assert.assertNotNull(properties);
    Assert.assertEquals(1, properties.size());
}

From source file:fr.xebia.servlet.filter.ExpiresFilterTest.java

@Test
public void testSkipBecauseExpiresIsDefined() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//  ww  w.  j  av  a2  s  .c om
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.addDateHeader("Expires", System.currentTimeMillis());
            response.getWriter().print("Hello world");
        }
    };

    validate(servlet, null);
}

From source file:fr.xebia.servlet.filter.ExpiresFilterTest.java

@Test
public void testUseContentTypeExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//from  w ww .  j a  v  a 2 s  . c  om
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.getWriter().print("Hello world");
        }
    };

    int expectedMaxAgeInSeconds = 3 * 60;

    validate(servlet, expectedMaxAgeInSeconds);
}

From source file:fr.xebia.servlet.filter.ExpiresFilterTest.java

@Test
public void testUseContentTypeWithoutCharsetExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override/* w w w .ja v  a  2s .  c o m*/
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/xml; charset=iso-8859-1");
            response.getWriter().print("Hello world");
        }
    };

    int expectedMaxAgeInSeconds = 5 * 60;

    validate(servlet, expectedMaxAgeInSeconds);
}

From source file:fr.xebia.servlet.filter.ExpiresFilterTest.java

@Test
public void testUseDefaultConfiguration1() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override//from w  w  w  . ja v a 2 s  .  c o  m
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("image/jpeg");
            response.getWriter().print("Hello world");
        }
    };

    int expectedMaxAgeInSeconds = 1 * 60;

    validate(servlet, expectedMaxAgeInSeconds);
}