Java Code Examples for org.springframework.http.server.ServerHttpRequest#getHeaders()
The following examples show how to use
org.springframework.http.server.ServerHttpRequest#getHeaders() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketConfig.java From bearchoke with Apache License 2.0 | 6 votes |
@Override protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) { Principal result = null; String authToken = null; HttpHeaders headers = request.getHeaders(); if (log.isDebugEnabled()) { log.debug("Determining user..."); } if (headers.containsKey(ServerConstants.X_AUTH_TOKEN)) { authToken = headers.getFirst(ServerConstants.X_AUTH_TOKEN); authenticate(authToken); } else if (attributes.containsKey(ServerConstants.X_AUTH_TOKEN)) { authToken = (String) attributes.get(ServerConstants.X_AUTH_TOKEN); authenticate(authToken); } else { result = super.determineUser(request, wsHandler, attributes); } return result; }
Example 2
Source File: WebSocketConfig.java From bearchoke with Apache License 2.0 | 6 votes |
@Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { if (request instanceof ServletServerHttpRequest) { HttpHeaders headers = request.getHeaders(); if (headers.containsKey(ServerConstants.X_AUTH_TOKEN)) { List<String> authToken = headers.get(ServerConstants.X_AUTH_TOKEN); if (log.isDebugEnabled()) { log.debug("Header auth token: " + authToken.get(0)); } attributes.put(ServerConstants.X_AUTH_TOKEN, headers.get(ServerConstants.X_AUTH_TOKEN)); } } return true; }
Example 3
Source File: AbstractStandardUpgradeStrategy.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public void upgrade(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user, WebSocketHandler wsHandler, Map<String, Object> attrs) throws HandshakeFailureException { HttpHeaders headers = request.getHeaders(); InetSocketAddress localAddr = request.getLocalAddress(); InetSocketAddress remoteAddr = request.getRemoteAddress(); StandardWebSocketSession session = new StandardWebSocketSession(headers, attrs, localAddr, remoteAddr, user); StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session); List<Extension> extensions = new ArrayList<Extension>(); for (WebSocketExtension extension : selectedExtensions) { extensions.add(new WebSocketToStandardExtensionAdapter(extension)); } upgradeInternal(request, response, selectedProtocol, extensions, endpoint); }
Example 4
Source File: RestJsonWrapperAdvice.java From bird-java with MIT License | 5 votes |
@Override public Object beforeBodyWrite(Object value, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> converter, ServerHttpRequest request, ServerHttpResponse response) { //返回结果时移除当前线程中的session数据 SessionContext.removeSession(); HttpHeaders headers = request.getHeaders(); if(ArrayUtils.contains(INNER_CALL_HEADER,headers.getFirst("call"))){ return value; } if(!(value instanceof OperationResult)){ value = OperationResult.Success("success", value); } return value; }
Example 5
Source File: RequestPartServletServerHttpRequestTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void getContentType() throws Exception { MultipartFile part = new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8")); this.mockRequest.addFile(part); ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part"); HttpHeaders headers = request.getHeaders(); assertNotNull(headers); assertEquals(MediaType.APPLICATION_JSON, headers.getContentType()); }
Example 6
Source File: AbstractHttpSockJsSession.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Handle the first request for receiving messages on a SockJS HTTP transport * based session. * <p>Long polling-based transports (e.g. "xhr", "jsonp") complete the request * after writing the open frame. Streaming-based transports ("xhr_streaming", * "eventsource", and "htmlfile") leave the response open longer for further * streaming of message frames but will also close it eventually after some * amount of data has been sent. * @param request the current request * @param response the current response * @param frameFormat the transport-specific SocksJS frame format to use */ public void handleInitialRequest(ServerHttpRequest request, ServerHttpResponse response, SockJsFrameFormat frameFormat) throws SockJsException { this.uri = request.getURI(); this.handshakeHeaders = request.getHeaders(); this.principal = request.getPrincipal(); this.localAddress = request.getLocalAddress(); this.remoteAddress = request.getRemoteAddress(); synchronized (this.responseLock) { try { this.response = response; this.frameFormat = frameFormat; this.asyncRequestControl = request.getAsyncRequestControl(response); this.asyncRequestControl.start(-1); disableShallowEtagHeaderFilter(request); // Let "our" handler know before sending the open frame to the remote handler delegateConnectionEstablished(); handleRequestInternal(request, response, true); // Request might have been reset (e.g. polling sessions do after writing) this.readyToSend = isActive(); } catch (Throwable ex) { tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); throw new SockJsTransportFailureException("Failed to open session", getId(), ex); } } }
Example 7
Source File: RequestPartServletServerHttpRequestTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void getContentType() throws Exception { MultipartFile part = new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8")); this.mockRequest.addFile(part); ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part"); HttpHeaders headers = request.getHeaders(); assertNotNull(headers); assertEquals(MediaType.APPLICATION_JSON, headers.getContentType()); }
Example 8
Source File: RequestPartServletServerHttpRequestTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void getContentType() throws Exception { MultipartFile part = new MockMultipartFile("part", "", "application/json", "content".getBytes("UTF-8")); this.mockRequest.addFile(part); ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part"); HttpHeaders headers = request.getHeaders(); assertNotNull(headers); assertEquals(MediaType.APPLICATION_JSON, headers.getContentType()); }
Example 9
Source File: DefaultCorsProcessor.java From java-technology-stack with MIT License | 4 votes |
private List<String> getHeadersToUse(ServerHttpRequest request, boolean isPreFlight) { HttpHeaders headers = request.getHeaders(); return (isPreFlight ? headers.getAccessControlRequestHeaders() : new ArrayList<>(headers.keySet())); }
Example 10
Source File: DefaultCorsProcessor.java From lams with GNU General Public License v2.0 | 4 votes |
private List<String> getHeadersToUse(ServerHttpRequest request, boolean isPreFlight) { HttpHeaders headers = request.getHeaders(); return (isPreFlight ? headers.getAccessControlRequestHeaders() : new ArrayList<String>(headers.keySet())); }
Example 11
Source File: AbstractTyrusRequestUpgradeStrategy.java From spring-analysis-note with MIT License | 4 votes |
@Override public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<Extension> extensions, Endpoint endpoint) throws HandshakeFailureException { HttpServletRequest servletRequest = getHttpServletRequest(request); HttpServletResponse servletResponse = getHttpServletResponse(response); TyrusServerContainer serverContainer = (TyrusServerContainer) getContainer(servletRequest); TyrusWebSocketEngine engine = (TyrusWebSocketEngine) serverContainer.getWebSocketEngine(); Object tyrusEndpoint = null; boolean success; try { // Shouldn't matter for processing but must be unique String path = "/" + random.nextLong(); tyrusEndpoint = createTyrusEndpoint(endpoint, path, selectedProtocol, extensions, serverContainer, engine); register(engine, tyrusEndpoint); HttpHeaders headers = request.getHeaders(); RequestContext requestContext = createRequestContext(servletRequest, path, headers); TyrusUpgradeResponse upgradeResponse = new TyrusUpgradeResponse(); UpgradeInfo upgradeInfo = engine.upgrade(requestContext, upgradeResponse); success = SUCCESS.equals(upgradeInfo.getStatus()); if (success) { if (logger.isTraceEnabled()) { logger.trace("Successful request upgrade: " + upgradeResponse.getHeaders()); } handleSuccess(servletRequest, servletResponse, upgradeInfo, upgradeResponse); } } catch (Exception ex) { unregisterTyrusEndpoint(engine, tyrusEndpoint); throw new HandshakeFailureException("Error during handshake: " + request.getURI(), ex); } unregisterTyrusEndpoint(engine, tyrusEndpoint); if (!success) { throw new HandshakeFailureException("Unexpected handshake failure: " + request.getURI()); } }
Example 12
Source File: AbstractTyrusRequestUpgradeStrategy.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, List<Extension> extensions, Endpoint endpoint) throws HandshakeFailureException { HttpServletRequest servletRequest = getHttpServletRequest(request); HttpServletResponse servletResponse = getHttpServletResponse(response); TyrusServerContainer serverContainer = (TyrusServerContainer) getContainer(servletRequest); TyrusWebSocketEngine engine = (TyrusWebSocketEngine) serverContainer.getWebSocketEngine(); Object tyrusEndpoint = null; boolean success; try { // Shouldn't matter for processing but must be unique String path = "/" + random.nextLong(); tyrusEndpoint = createTyrusEndpoint(endpoint, path, selectedProtocol, extensions, serverContainer, engine); getEndpointHelper().register(engine, tyrusEndpoint); HttpHeaders headers = request.getHeaders(); RequestContext requestContext = createRequestContext(servletRequest, path, headers); TyrusUpgradeResponse upgradeResponse = new TyrusUpgradeResponse(); UpgradeInfo upgradeInfo = engine.upgrade(requestContext, upgradeResponse); success = SUCCESS.equals(upgradeInfo.getStatus()); if (success) { if (logger.isTraceEnabled()) { logger.trace("Successful request upgrade: " + upgradeResponse.getHeaders()); } handleSuccess(servletRequest, servletResponse, upgradeInfo, upgradeResponse); } } catch (Exception ex) { unregisterTyrusEndpoint(engine, tyrusEndpoint); throw new HandshakeFailureException("Error during handshake: " + request.getURI(), ex); } unregisterTyrusEndpoint(engine, tyrusEndpoint); if (!success) { throw new HandshakeFailureException("Unexpected handshake failure: " + request.getURI()); } }
Example 13
Source File: AbstractHandshakeHandler.java From java-technology-stack with MIT License | 4 votes |
@Override public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException { WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHeaders()); if (logger.isTraceEnabled()) { logger.trace("Processing request " + request.getURI() + " with headers=" + headers); } try { if (HttpMethod.GET != request.getMethod()) { response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED); response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET)); if (logger.isErrorEnabled()) { logger.error("Handshake failed due to unexpected HTTP method: " + request.getMethod()); } return false; } if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) { handleInvalidUpgradeHeader(request, response); return false; } if (!headers.getConnection().contains("Upgrade") && !headers.getConnection().contains("upgrade")) { handleInvalidConnectHeader(request, response); return false; } if (!isWebSocketVersionSupported(headers)) { handleWebSocketVersionNotSupported(request, response); return false; } if (!isValidOrigin(request)) { response.setStatusCode(HttpStatus.FORBIDDEN); return false; } String wsKey = headers.getSecWebSocketKey(); if (wsKey == null) { if (logger.isErrorEnabled()) { logger.error("Missing \"Sec-WebSocket-Key\" header"); } response.setStatusCode(HttpStatus.BAD_REQUEST); return false; } } catch (IOException ex) { throw new HandshakeFailureException( "Response update failed during upgrade to WebSocket: " + request.getURI(), ex); } String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler); List<WebSocketExtension> requested = headers.getSecWebSocketExtensions(); List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request); List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported); Principal user = determineUser(request, wsHandler, attributes); if (logger.isTraceEnabled()) { logger.trace("Upgrading to WebSocket, subProtocol=" + subProtocol + ", extensions=" + extensions); } this.requestUpgradeStrategy.upgrade(request, response, subProtocol, extensions, user, wsHandler, attributes); return true; }
Example 14
Source File: AbstractHandshakeHandler.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException { WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHeaders()); if (logger.isTraceEnabled()) { logger.trace("Processing request " + request.getURI() + " with headers=" + headers); } try { if (HttpMethod.GET != request.getMethod()) { response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED); response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET)); if (logger.isErrorEnabled()) { logger.error("Handshake failed due to unexpected HTTP method: " + request.getMethod()); } return false; } if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) { handleInvalidUpgradeHeader(request, response); return false; } if (!headers.getConnection().contains("Upgrade") && !headers.getConnection().contains("upgrade")) { handleInvalidConnectHeader(request, response); return false; } if (!isWebSocketVersionSupported(headers)) { handleWebSocketVersionNotSupported(request, response); return false; } if (!isValidOrigin(request)) { response.setStatusCode(HttpStatus.FORBIDDEN); return false; } String wsKey = headers.getSecWebSocketKey(); if (wsKey == null) { if (logger.isErrorEnabled()) { logger.error("Missing \"Sec-WebSocket-Key\" header"); } response.setStatusCode(HttpStatus.BAD_REQUEST); return false; } } catch (IOException ex) { throw new HandshakeFailureException( "Response update failed during upgrade to WebSocket: " + request.getURI(), ex); } String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler); List<WebSocketExtension> requested = headers.getSecWebSocketExtensions(); List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request); List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported); Principal user = determineUser(request, wsHandler, attributes); if (logger.isTraceEnabled()) { logger.trace("Upgrading to WebSocket, subProtocol=" + subProtocol + ", extensions=" + extensions); } this.requestUpgradeStrategy.upgrade(request, response, subProtocol, extensions, user, wsHandler, attributes); return true; }
Example 15
Source File: AbstractTyrusRequestUpgradeStrategy.java From java-technology-stack with MIT License | 4 votes |
@Override public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<Extension> extensions, Endpoint endpoint) throws HandshakeFailureException { HttpServletRequest servletRequest = getHttpServletRequest(request); HttpServletResponse servletResponse = getHttpServletResponse(response); TyrusServerContainer serverContainer = (TyrusServerContainer) getContainer(servletRequest); TyrusWebSocketEngine engine = (TyrusWebSocketEngine) serverContainer.getWebSocketEngine(); Object tyrusEndpoint = null; boolean success; try { // Shouldn't matter for processing but must be unique String path = "/" + random.nextLong(); tyrusEndpoint = createTyrusEndpoint(endpoint, path, selectedProtocol, extensions, serverContainer, engine); register(engine, tyrusEndpoint); HttpHeaders headers = request.getHeaders(); RequestContext requestContext = createRequestContext(servletRequest, path, headers); TyrusUpgradeResponse upgradeResponse = new TyrusUpgradeResponse(); UpgradeInfo upgradeInfo = engine.upgrade(requestContext, upgradeResponse); success = SUCCESS.equals(upgradeInfo.getStatus()); if (success) { if (logger.isTraceEnabled()) { logger.trace("Successful request upgrade: " + upgradeResponse.getHeaders()); } handleSuccess(servletRequest, servletResponse, upgradeInfo, upgradeResponse); } } catch (Exception ex) { unregisterTyrusEndpoint(engine, tyrusEndpoint); throw new HandshakeFailureException("Error during handshake: " + request.getURI(), ex); } unregisterTyrusEndpoint(engine, tyrusEndpoint); if (!success) { throw new HandshakeFailureException("Unexpected handshake failure: " + request.getURI()); } }
Example 16
Source File: DefaultCorsProcessor.java From spring4-understanding with Apache License 2.0 | 4 votes |
private List<String> getHeadersToUse(ServerHttpRequest request, boolean isPreFlight) { HttpHeaders headers = request.getHeaders(); return (isPreFlight ? headers.getAccessControlRequestHeaders() : new ArrayList<String>(headers.keySet())); }
Example 17
Source File: DefaultCorsProcessor.java From spring-analysis-note with MIT License | 4 votes |
private List<String> getHeadersToUse(ServerHttpRequest request, boolean isPreFlight) { HttpHeaders headers = request.getHeaders(); return (isPreFlight ? headers.getAccessControlRequestHeaders() : new ArrayList<>(headers.keySet())); }
Example 18
Source File: AbstractHandshakeHandler.java From spring-analysis-note with MIT License | 4 votes |
@Override public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException { WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHeaders()); if (logger.isTraceEnabled()) { logger.trace("Processing request " + request.getURI() + " with headers=" + headers); } try { if (HttpMethod.GET != request.getMethod()) { response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED); response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET)); if (logger.isErrorEnabled()) { logger.error("Handshake failed due to unexpected HTTP method: " + request.getMethod()); } return false; } if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) { handleInvalidUpgradeHeader(request, response); return false; } if (!headers.getConnection().contains("Upgrade") && !headers.getConnection().contains("upgrade")) { handleInvalidConnectHeader(request, response); return false; } if (!isWebSocketVersionSupported(headers)) { handleWebSocketVersionNotSupported(request, response); return false; } if (!isValidOrigin(request)) { response.setStatusCode(HttpStatus.FORBIDDEN); return false; } String wsKey = headers.getSecWebSocketKey(); if (wsKey == null) { if (logger.isErrorEnabled()) { logger.error("Missing \"Sec-WebSocket-Key\" header"); } response.setStatusCode(HttpStatus.BAD_REQUEST); return false; } } catch (IOException ex) { throw new HandshakeFailureException( "Response update failed during upgrade to WebSocket: " + request.getURI(), ex); } String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler); List<WebSocketExtension> requested = headers.getSecWebSocketExtensions(); List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request); List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported); Principal user = determineUser(request, wsHandler, attributes); if (logger.isTraceEnabled()) { logger.trace("Upgrading to WebSocket, subProtocol=" + subProtocol + ", extensions=" + extensions); } this.requestUpgradeStrategy.upgrade(request, response, subProtocol, extensions, user, wsHandler, attributes); return true; }