Search in sources :

Example 56 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project tomcat by apache.

the class TestExpiresFilter method testUseMajorTypeExpiresConfiguration.

@Test
public void testUseMajorTypeExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/json; charset=iso-8859-1");
            response.getWriter().print("Hello world");
        }
    };
    validate(servlet, Integer.valueOf(7 * 60));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 57 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project tomcat by apache.

the class AsyncStockServlet method writeStock.

public void writeStock(AsyncContext actx, Stock stock) {
    HttpServletResponse response = (HttpServletResponse) actx.getResponse();
    try {
        PrintWriter writer = response.getWriter();
        //make client parsing easier
        writer.write("STOCK#");
        writer.write(stock.getSymbol());
        writer.write("#");
        writer.write(stock.getValueAsString());
        writer.write("#");
        writer.write(stock.getLastChangeAsString());
        writer.write("#");
        writer.write(String.valueOf(stock.getCnt()));
        writer.write("\n");
        writer.flush();
        response.flushBuffer();
    } catch (IOException x) {
        try {
            actx.complete();
        } catch (Exception ignore) {
        /* Ignore */
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 58 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project zeppelin by apache.

the class CorsFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    String sourceHost = ((HttpServletRequest) request).getHeader("Origin");
    String origin = "";
    try {
        if (SecurityUtils.isValidOrigin(sourceHost, ZeppelinConfiguration.create())) {
            origin = sourceHost;
        }
    } catch (URISyntaxException e) {
        LOGGER.error("Exception in WebDriverManager while getWebDriver ", e);
    }
    if (((HttpServletRequest) request).getMethod().equals("OPTIONS")) {
        HttpServletResponse resp = ((HttpServletResponse) response);
        addCorsHeaders(resp, origin);
        return;
    }
    if (response instanceof HttpServletResponse) {
        HttpServletResponse alteredResponse = ((HttpServletResponse) response);
        addCorsHeaders(alteredResponse, origin);
    }
    filterChain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) URISyntaxException(java.net.URISyntaxException)

Example 59 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project zeppelin by apache.

the class CorsFilterTest method InvalidCorsFilterTest.

@Test
@SuppressWarnings("rawtypes")
public void InvalidCorsFilterTest() throws IOException, ServletException {
    CorsFilter filter = new CorsFilter();
    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    FilterChain mockedFilterChain = mock(FilterChain.class);
    HttpServletRequest mockRequest = mock(HttpServletRequest.class);
    when(mockRequest.getHeader("Origin")).thenReturn("http://evillocalhost:8080");
    when(mockRequest.getMethod()).thenReturn("Empty");
    when(mockRequest.getServerName()).thenReturn("evillocalhost");
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            headers[count] = invocationOnMock.getArguments()[1].toString();
            count++;
            return null;
        }
    }).when(mockResponse).addHeader(anyString(), anyString());
    filter.doFilter(mockRequest, mockResponse, mockedFilterChain);
    Assert.assertTrue(headers[0].equals(""));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 60 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project cas by apereo.

the class LogoutAction method doInternalExecute.

@Override
protected Event doInternalExecute(final HttpServletRequest request, final HttpServletResponse response, final RequestContext context) throws Exception {
    boolean needFrontSlo = false;
    final List<LogoutRequest> logoutRequests = WebUtils.getLogoutRequests(context);
    if (logoutRequests != null) {
        // if some logout request must still be attempted
        needFrontSlo = logoutRequests.stream().anyMatch(logoutRequest -> logoutRequest.getStatus() == LogoutRequestStatus.NOT_ATTEMPTED);
    }
    final String paramName = StringUtils.defaultIfEmpty(logoutProperties.getRedirectParameter(), CasProtocolConstants.PARAMETER_SERVICE);
    LOGGER.debug("Using parameter name [{}] to detect destination service, if any", paramName);
    final String service = request.getParameter(paramName);
    LOGGER.debug("Located target service [{}] for redirection after logout", paramName);
    if (logoutProperties.isFollowServiceRedirects() && StringUtils.isNotBlank(service)) {
        final Service webAppService = webApplicationServiceFactory.createService(service);
        final RegisteredService rService = this.servicesManager.findServiceBy(webAppService);
        if (rService != null && rService.getAccessStrategy().isServiceAccessAllowed()) {
            LOGGER.debug("Redirecting to service [{}]", service);
            WebUtils.putLogoutRedirectUrl(context, service);
        } else {
            LOGGER.warn("Cannot redirect to [{}] given the service is unauthorized to use CAS. " + "Ensure the service is registered with CAS and is enabled to allowed access", service);
        }
    } else {
        LOGGER.debug("No target service is located for redirection after logout, or CAS is not allowed to follow redirects after logout");
    }
    // there are some front services to logout, perform front SLO
    if (needFrontSlo) {
        LOGGER.debug("Proceeding forward with front-channel single logout");
        return new Event(this, FRONT_EVENT);
    }
    LOGGER.debug("Moving forward to finish the logout process");
    return new Event(this, FINISH_EVENT);
}
Also used : CasProtocolConstants(org.apereo.cas.CasProtocolConstants) LogoutRequest(org.apereo.cas.logout.LogoutRequest) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) LogoutRequestStatus(org.apereo.cas.logout.LogoutRequestStatus) LogoutProperties(org.apereo.cas.configuration.model.core.logout.LogoutProperties) StringUtils(org.apache.commons.lang3.StringUtils) RequestContext(org.springframework.webflow.execution.RequestContext) RegisteredService(org.apereo.cas.services.RegisteredService) WebApplicationService(org.apereo.cas.authentication.principal.WebApplicationService) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) Service(org.apereo.cas.authentication.principal.Service) WebUtils(org.apereo.cas.web.support.WebUtils) ServiceFactory(org.apereo.cas.authentication.principal.ServiceFactory) Event(org.springframework.webflow.execution.Event) ServicesManager(org.apereo.cas.services.ServicesManager) RegisteredService(org.apereo.cas.services.RegisteredService) RegisteredService(org.apereo.cas.services.RegisteredService) WebApplicationService(org.apereo.cas.authentication.principal.WebApplicationService) Service(org.apereo.cas.authentication.principal.Service) Event(org.springframework.webflow.execution.Event) LogoutRequest(org.apereo.cas.logout.LogoutRequest)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)1635 HttpServletRequest (javax.servlet.http.HttpServletRequest)1312 Test (org.junit.Test)705 IOException (java.io.IOException)576 ServletException (javax.servlet.ServletException)491 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)223 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)195 Request (org.eclipse.jetty.server.Request)186 HttpServlet (javax.servlet.http.HttpServlet)157 CountDownLatch (java.util.concurrent.CountDownLatch)156 FilterChain (javax.servlet.FilterChain)148 PrintWriter (java.io.PrintWriter)138 Test (org.testng.annotations.Test)127 HashMap (java.util.HashMap)106 ServletOutputStream (javax.servlet.ServletOutputStream)105 InterruptedIOException (java.io.InterruptedIOException)97 InputStream (java.io.InputStream)85 OutputStream (java.io.OutputStream)81 HttpSession (javax.servlet.http.HttpSession)75 ServletResponse (javax.servlet.ServletResponse)74