Java Code Examples for org.springframework.http.server.ServerHttpRequest#getMethod()

The following examples show how to use org.springframework.http.server.ServerHttpRequest#getMethod() . 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: AbstractSockJsService.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
	if (request.getMethod() == HttpMethod.GET) {
		addNoCacheHeaders(response);
		if (checkOrigin(request, response)) {
			response.getHeaders().setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
			String content = String.format(
					INFO_CONTENT, random.nextInt(), isSessionCookieNeeded(), isWebSocketEnabled());
			response.getBody().write(content.getBytes());
		}

	}
	else if (request.getMethod() == HttpMethod.OPTIONS) {
		if (checkOrigin(request, response)) {
			addCacheHeaders(response);
			response.setStatusCode(HttpStatus.NO_CONTENT);
		}
	}
	else {
		sendMethodNotAllowed(response, HttpMethod.GET, HttpMethod.OPTIONS);
	}
}
 
Example 2
Source File: AbstractSockJsService.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
	if (request.getMethod() == HttpMethod.GET) {
		addNoCacheHeaders(response);
		if (checkOrigin(request, response)) {
			response.getHeaders().setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
			String content = String.format(
					INFO_CONTENT, random.nextInt(), isSessionCookieNeeded(), isWebSocketEnabled());
			response.getBody().write(content.getBytes());
		}

	}
	else if (request.getMethod() == HttpMethod.OPTIONS) {
		if (checkOrigin(request, response)) {
			addCacheHeaders(response);
			response.setStatusCode(HttpStatus.NO_CONTENT);
		}
	}
	else {
		sendMethodNotAllowed(response, HttpMethod.GET, HttpMethod.OPTIONS);
	}
}
 
Example 3
Source File: AbstractSockJsService.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
	if (HttpMethod.GET == request.getMethod()) {
		addNoCacheHeaders(response);
		if (checkOrigin(request, response)) {
			response.getHeaders().setContentType(new MediaType("application", "json", UTF8_CHARSET));
			String content = String.format(
					INFO_CONTENT, random.nextInt(), isSessionCookieNeeded(), isWebSocketEnabled());
			response.getBody().write(content.getBytes());
		}

	}
	else if (HttpMethod.OPTIONS == request.getMethod()) {
		if (checkOrigin(request, response)) {
			addCacheHeaders(response);
			response.setStatusCode(HttpStatus.NO_CONTENT);
		}
	}
	else {
		sendMethodNotAllowed(response, HttpMethod.OPTIONS, HttpMethod.GET);
	}
}
 
Example 4
Source File: AbstractSockJsService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
	if (request.getMethod() != HttpMethod.GET) {
		sendMethodNotAllowed(response, HttpMethod.GET);
		return;
	}

	String content = String.format(IFRAME_CONTENT, getSockJsClientLibraryUrl());
	byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
	StringBuilder builder = new StringBuilder("\"0");
	DigestUtils.appendMd5DigestAsHex(contentBytes, builder);
	builder.append('"');
	String etagValue = builder.toString();

	List<String> ifNoneMatch = request.getHeaders().getIfNoneMatch();
	if (!CollectionUtils.isEmpty(ifNoneMatch) && ifNoneMatch.get(0).equals(etagValue)) {
		response.setStatusCode(HttpStatus.NOT_MODIFIED);
		return;
	}

	response.getHeaders().setContentType(new MediaType("text", "html", StandardCharsets.UTF_8));
	response.getHeaders().setContentLength(contentBytes.length);

	// No cache in order to check every time if IFrame are authorized
	addNoCacheHeaders(response);
	response.getHeaders().setETag(etagValue);
	response.getBody().write(contentBytes);
}
 
Example 5
Source File: AbstractSockJsService.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
	if (request.getMethod() != HttpMethod.GET) {
		sendMethodNotAllowed(response, HttpMethod.GET);
		return;
	}

	String content = String.format(IFRAME_CONTENT, getSockJsClientLibraryUrl());
	byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
	StringBuilder builder = new StringBuilder("\"0");
	DigestUtils.appendMd5DigestAsHex(contentBytes, builder);
	builder.append('"');
	String etagValue = builder.toString();

	List<String> ifNoneMatch = request.getHeaders().getIfNoneMatch();
	if (!CollectionUtils.isEmpty(ifNoneMatch) && ifNoneMatch.get(0).equals(etagValue)) {
		response.setStatusCode(HttpStatus.NOT_MODIFIED);
		return;
	}

	response.getHeaders().setContentType(new MediaType("text", "html", StandardCharsets.UTF_8));
	response.getHeaders().setContentLength(contentBytes.length);

	// No cache in order to check every time if IFrame are authorized
	addNoCacheHeaders(response);
	response.getHeaders().setETag(etagValue);
	response.getBody().write(contentBytes);
}
 
Example 6
Source File: AbstractHandshakeHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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 7
Source File: DefaultCorsProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Nullable
private HttpMethod getMethodToUse(ServerHttpRequest request, boolean isPreFlight) {
	return (isPreFlight ? request.getHeaders().getAccessControlRequestMethod() : request.getMethod());
}
 
Example 8
Source File: AbstractHandshakeHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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 9
Source File: DefaultCorsProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Nullable
private HttpMethod getMethodToUse(ServerHttpRequest request, boolean isPreFlight) {
	return (isPreFlight ? request.getHeaders().getAccessControlRequestMethod() : request.getMethod());
}
 
Example 10
Source File: DefaultCorsProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private HttpMethod getMethodToUse(ServerHttpRequest request, boolean isPreFlight) {
	return (isPreFlight ? request.getHeaders().getAccessControlRequestMethod() : request.getMethod());
}
 
Example 11
Source File: AbstractHandshakeHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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 12
Source File: DefaultCorsProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private HttpMethod getMethodToUse(ServerHttpRequest request, boolean isPreFlight) {
	return (isPreFlight ? request.getHeaders().getAccessControlRequestMethod() : request.getMethod());
}