Refine search
private Principal[] getGrantedRoles(Account account, Deployment deployment) { if (account == null) { return new Principal[] {}; } Set<String> roles = new HashSet<>(account.getRoles()); Map<String, Set<String>> principalVersusRolesMap = deployment.getDeploymentInfo().getPrincipalVersusRolesMap(); roles.addAll(principalVersusRolesMap.getOrDefault(account.getPrincipal().getName(), Collections.emptySet())); Principal[] principals = new Principal[roles.size()]; int index = 0; for (String role : roles) { principals[index++] = () -> role; } return principals; } }
registerServletContainerInitializerToDriveServletContextInitializers(deployment, initializers); deployment.setClassLoader(getServletClassLoader()); deployment.setContextPath(getContextPath()); deployment.setDisplayName(getDisplayName()); deployment.setDeploymentName("spring-boot"); if (isRegisterDefaultServlet()) { DeploymentManager manager = Servlets.newContainer().addDeployment(deployment); manager.deploy(); SessionManager sessionManager = manager.getDeployment().getSessionManager(); Duration timeoutDuration = getSession().getTimeout(); int sessionTimeout = (isZeroOrLess(timeoutDuration) ? -1
@Override public ServletContext getServletContext() { return this.manager.getDeployment().getServletContext(); }
final String path = exchange.getRelativePath(); if(isForbiddenPath(path)) { exchange.setResponseCode(StatusCodes.NOT_FOUND); return; String upgradeString = exchange.getRequestHeaders().getFirst(Headers.UPGRADE); boolean isUpgradeRequest = upgradeString != null && !upgradeString.startsWith(HTTP2_UPGRADE_PREFIX); if (info.getType() == ServletPathMatch.Type.REDIRECT && !isUpgradeRequest) { executor = convergedServletContext.getDeployment().getExecutor();
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { final String path = exchange.getRelativePath(); if(isForbiddenPath(path)) { exchange.setStatusCode(StatusCodes.NOT_FOUND); return; String upgradeString = exchange.getRequestHeaders().getFirst(Headers.UPGRADE); boolean isUpgradeRequest = upgradeString != null && !upgradeString.startsWith(HTTP2_UPGRADE_PREFIX); if (info.getType() == ServletPathMatch.Type.REDIRECT && !isUpgradeRequest) { executor = servletContext.getDeployment().getExecutor();
private ServletContext buildServletContext(String contextName) throws ServletException { ServletContainer servletContainer = new ServletContainerImpl(); DeploymentInfo deploymentInfo = new DeploymentInfo(); deploymentInfo.setClassLoader(Thread.currentThread().getContextClassLoader()); deploymentInfo.setDeploymentName("cxf-undertow"); deploymentInfo.setContextPath(contextName); ServletInfo asyncServlet = new ServletInfo(ServletPathMatches.DEFAULT_SERVLET_NAME, CxfUndertowServlet.class); deploymentInfo.addServlet(asyncServlet); servletContainer.addDeployment(deploymentInfo); DeploymentManager deploymentManager = servletContainer.getDeployment(deploymentInfo.getDeploymentName()); deploymentManager.deploy(); deploymentManager.start(); return deploymentManager.getDeployment().getServletContext(); }
onAsyncError(error); if (!dispatched) { if(!exchange.isResponseStarted()) { exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR); exchange.getResponseHeaders().clear(); UndertowLogger.REQUEST_IO_LOGGER.ioException((IOException) error); } else { ExceptionHandler exceptionHandler = servletRequestContext.getDeployment().getDeploymentInfo().getExceptionHandler(); if(exceptionHandler == null) { exceptionHandler = LoggingExceptionHandler.DEFAULT;
@Override protected SessionManager getSessionManager() { ServletRequestContext servletRequestContext = httpServerExchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); return servletRequestContext.getDeployment().getSessionManager(); }
@Override public void sendError(final int sc, final String msg) throws IOException { if(insideInclude) { //not 100% sure this is the correct action return; } ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); if (responseStarted()) { if(src.getErrorCode() > 0) { return; //error already set } throw UndertowServletMessages.MESSAGES.responseAlreadyCommited(); } if(servletContext.getDeployment().getDeploymentInfo().isSendCustomReasonPhraseOnError()) { exchange.setReasonPhrase(msg); } writer = null; responseState = ResponseState.NONE; exchange.setStatusCode(sc); if(src.isRunningInsideHandler()) { //all we do is set the error on the context, we handle it when the request is returned treatAsCommitted = true; src.setError(sc, msg); } else { //if the src is null there is no outer handler, as we are in an asnc request doErrorDispatch(sc, msg); } }
@Override public boolean resolve(final HttpServerExchange value) { String location = this.location.readAttribute(value); ServletRequestContext src = value.getAttachment(ServletRequestContext.ATTACHMENT_KEY); if(src == null) { return false; } ResourceManager manager = src.getDeployment().getDeploymentInfo().getResourceManager(); if(manager == null) { return false; } try { Resource resource = manager.getResource(location); if(resource == null) { return false; } return resource.isDirectory(); } catch (IOException e) { throw new RuntimeException(e); } }
public boolean displayStackTraces() { ServletStackTraces mode = deployment.getDeploymentInfo().getServletStackTraces(); if (mode == ServletStackTraces.NONE) { return false; } else if (mode == ServletStackTraces.ALL) { return true; } else { InetSocketAddress localAddress = getExchange().getSourceAddress(); if(localAddress == null) { return false; } InetAddress address = localAddress.getAddress(); if(address == null) { return false; } if(!address.isLoopbackAddress()) { return false; } return !getExchange().getRequestHeaders().contains(Headers.X_FORWARDED_FOR); } }
final ServletInfo servletInfo = Servlets.servlet(EndpointServlet.NAME, EndpointServlet.class).addMapping("/*") .setAsyncSupported(true); CamelLogger.LOGGER.debug("Deploying endpoint {}", endPointDeplyomentInfo.getDeploymentName()); Thread.currentThread().setContextClassLoader(endPointDeplyomentInfo.getClassLoader()); try { final DeploymentManager manager = servletContainerServiceSupplier.getValue().getServletContainer() deploymentConsumer.accept((DeploymentImpl) deployment); manager.start(); hostSupplier.getValue().registerDeployment(deployment, deployment.getHandler()); ManagedServlet managedServlet = deployment.getServlets().getManagedServlet(EndpointServlet.NAME);
@Override public void setLocale(final Locale loc) { if (insideInclude || responseStarted()) { return; } this.locale = loc; exchange.getResponseHeaders().put(Headers.CONTENT_LANGUAGE, loc.getLanguage() + "-" + loc.getCountry()); if (!charsetSet && writer == null) { final Map<String, String> localeCharsetMapping = servletContext.getDeployment().getDeploymentInfo().getLocaleCharsetMapping(); // Match full language_country_variant first, then language_country, // then language only String charset = localeCharsetMapping.get(locale.toString()); if (charset == null) { charset = localeCharsetMapping.get(locale.getLanguage() + "_" + locale.getCountry()); if (charset == null) { charset = localeCharsetMapping.get(locale.getLanguage()); } } if (charset != null) { this.charset = charset; if (contentType != null) { exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, getContentType()); } } } }
@Override public boolean isUserInRole(final String role) { if (role == null) { return false; } //according to the servlet spec this aways returns false if (role.equals("*")) { return false; } SecurityContext sc = exchange.getSecurityContext(); Account account = sc.getAuthenticatedAccount(); if (account == null) { return false; } ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); if (role.equals("**")) { Set<String> roles = servletRequestContext.getDeployment().getDeploymentInfo().getSecurityRoles(); if (!roles.contains("**")) { return true; } } final ServletChain servlet = servletRequestContext.getCurrentServlet(); final Deployment deployment = servletContext.getDeployment(); final AuthorizationManager authorizationManager = deployment.getDeploymentInfo().getAuthorizationManager(); return authorizationManager.isUserInRole(role, account, servlet.getManagedServlet().getServletInfo(), this, deployment); }
public void doErrorDispatch(int sc, String error) throws IOException { writer = null; responseState = ResponseState.NONE; resetBuffer(); treatAsCommitted = false; final String location = servletContext.getDeployment().getErrorPages().getErrorLocation(sc); if (location != null) { RequestDispatcherImpl requestDispatcher = new RequestDispatcherImpl(location, servletContext); final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); try { requestDispatcher.error(servletRequestContext, servletRequestContext.getServletRequest(), servletRequestContext.getServletResponse(), exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getCurrentServlet().getManagedServlet().getServletInfo().getName(), error); } catch (ServletException e) { throw new RuntimeException(e); } } else if (error != null) { setContentType("text/html"); setCharacterEncoding("UTF-8"); if(servletContext.getDeployment().getDeploymentInfo().isEscapeErrorMessage()) { getWriter().write("<html><head><title>Error</title></head><body>" + escapeHtml(error) + "</body></html>"); } else { getWriter().write("<html><head><title>Error</title></head><body>" + error + "</body></html>"); } getWriter().close(); } responseDone(); }
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); final AuthorizationManager authorizationManager = servletRequestContext.getDeployment().getDeploymentInfo().getAuthorizationManager(); TransportGuaranteeType connectionGuarantee = servletRequestContext.getOriginalRequest().isSecure() ? TransportGuaranteeType.CONFIDENTIAL : TransportGuaranteeType.NONE; TransportGuaranteeType transportGuarantee = authorizationManager.transportGuarantee(connectionGuarantee, servletRequestContext.getTransportGuarenteeType(), servletRequestContext.getOriginalRequest()); servletRequestContext.setTransportGuarenteeType(transportGuarantee); if (TransportGuaranteeType.REJECTED == transportGuarantee) { HttpServletResponse response = (HttpServletResponse) servletRequestContext.getServletResponse(); response.sendError(StatusCodes.FORBIDDEN); return; } super.handleRequest(exchange); }
@Override public AsyncContext startAsync(final ServletRequest servletRequest, final ServletResponse servletResponse) throws IllegalStateException { final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); if (!servletContext.getDeployment().getDeploymentInfo().isAllowNonStandardWrappers()) { if (servletRequestContext.getOriginalRequest() != servletRequest) { if (!(servletRequest instanceof ServletRequestWrapper)) { throw UndertowServletMessages.MESSAGES.requestWasNotOriginalOrWrapper(servletRequest); } } if (servletRequestContext.getOriginalResponse() != servletResponse) { if (!(servletResponse instanceof ServletResponseWrapper)) { throw UndertowServletMessages.MESSAGES.responseWasNotOriginalOrWrapper(servletResponse); } } } if (!isAsyncSupported()) { throw UndertowServletMessages.MESSAGES.startAsyncNotAllowed(); } else if (asyncStarted) { throw UndertowServletMessages.MESSAGES.asyncAlreadyStarted(); } asyncStarted = true; servletRequestContext.setServletRequest(servletRequest); servletRequestContext.setServletResponse(servletResponse); return asyncContext = new AsyncContextImpl(exchange, servletRequest, servletResponse, servletRequestContext, true, asyncContext); }
case AUTHENTICATED: if (isCacheable(notification)) { if(servletContext.getDeployment().getDeploymentInfo().isChangeSessionIdOnLogin()) { if (httpSession != null) { Session session = underlyingSession(httpSession); !httpSession.isInvalid() && session.getAttribute(NO_ID_CHANGE_REQUIRED) == null) { ServletRequestContext src = notification.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY); src.getOriginalRequest().changeSessionId();
private static HttpScope applicationScope(HttpServerExchange exchange) { ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); final ServletContext servletContext = deployment.getServletContext(); return new HttpScope() { @Override
@Override public ServletRegistration.Dynamic addServlet(final String servletName, final Servlet servlet) { ensureNotProgramaticListener(); ensureNotInitialized(); ensureServletNameNotNull(servletName); if (deploymentInfo.getServlets().containsKey(servletName)) { return null; } ServletInfo s = new ServletInfo(servletName, servlet.getClass(), new ImmediateInstanceFactory<>(servlet)); readServletAnnotations(s); deploymentInfo.addServlet(s); ServletHandler handler = deployment.getServlets().addServlet(s); return new ServletRegistrationImpl(s, handler.getManagedServlet(), deployment); }