本文整理汇总了Java中org.springframework.http.server.ServerHttpResponse类的典型用法代码示例。如果您正苦于以下问题:Java ServerHttpResponse类的具体用法?Java ServerHttpResponse怎么用?Java ServerHttpResponse使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServerHttpResponse类属于org.springframework.http.server包,在下文中一共展示了ServerHttpResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: beforeBodyWrite
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
ResponseJSONP responseJsonp = returnType.getMethodAnnotation(ResponseJSONP.class);
if(responseJsonp == null){
responseJsonp = returnType.getContainingClass().getAnnotation(ResponseJSONP.class);
}
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
String callbackMethodName = servletRequest.getParameter(responseJsonp.callback());
if (!IOUtils.isValidJsonpQueryParam(callbackMethodName)) {
if (logger.isDebugEnabled()) {
logger.debug("Invalid jsonp parameter value:" + callbackMethodName);
}
callbackMethodName = null;
}
JSONPObject jsonpObject = new JSONPObject(callbackMethodName);
jsonpObject.addParameter(body);
beforeBodyWriteInternal(jsonpObject, selectedContentType, returnType, request, response);
return jsonpObject;
}
示例2: beforeHandshake
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
System.out.println("before handshake");
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession(false);
Long uid = (Long)session.getAttribute("uid");
if(uid == null){
return false;
}
//如果uid==null,afterConnectionEstablished不执行,协议转换不成功,报下面的错误
//websocket.html?uid=1990:56 WebSocket connection to 'ws://localhost:8080/ws?uid=1990' failed: Error during WebSocket handshake: Unexpected response code: 500
attributes.put("uid", uid);
return true;
// 如果这里返回false,浏览器会报下面这个错误
// WebSocket connection to 'ws://localhost:8080/ws' failed: Error during WebSocket handshake: Unexpected response code: 200
}
示例3: beforeBodyWrite
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
Class converterType = returnType.getMethod().getDeclaringClass();
FilterResponse defaultResponse = returnType.getMethod().getAnnotation(FilterResponse.class);
if(defaultResponse==null) {
defaultResponse = (FilterResponse) converterType.getAnnotation(FilterResponse.class);
}
String className =converterType.getName();
logger.debug("beforeBodyWrite - className >" +className +", defaultResponse - > "+defaultResponse);
if(defaultResponse!=null){
switch (defaultResponse.type()){
case defaultFilter:{
response.getHeaders().add(Constants.defaultResponseHeader,Constants.defaultResponseHeader);
}
}
}
return body;
}
示例4: beforeBodyWriteInternal
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
@Override
protected void beforeBodyWriteInternal(final MappingJacksonValue bodyContainer,
final MediaType contentType,
final MethodParameter returnType,
final ServerHttpRequest request,
final ServerHttpResponse response) {
final Mutable<Class<?>> viewClass = Mutable.of(View.Anonymous.class);
if (SecurityContextHolder.getContext().getAuthentication() != null && SecurityContextHolder.getContext().getAuthentication().getAuthorities() != null) {
final Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
viewClass.mutateIf(View.User.class, authorities.stream().anyMatch(o -> o.getAuthority().equals("PRIV_USER")));
viewClass.mutateIf(View.Moderator.class, authorities.stream().anyMatch(o -> o.getAuthority().equals("PRIV_MODERATOR")));
viewClass.mutateIf(View.Admin.class, authorities.stream().anyMatch(o -> o.getAuthority().equals("PRIV_ADMIN")));
}
bodyContainer.setSerializationView(viewClass.get());
}
示例5: handleHttpEntityResponse
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) throws Exception {
if (responseEntity == null) {
return;
}
HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
HttpOutputMessage outputMessage = createHttpOutputMessage(webRequest);
if (responseEntity instanceof ResponseEntity && outputMessage instanceof ServerHttpResponse) {
((ServerHttpResponse) outputMessage).setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
}
HttpHeaders entityHeaders = responseEntity.getHeaders();
if (!entityHeaders.isEmpty()) {
outputMessage.getHeaders().putAll(entityHeaders);
}
Object body = responseEntity.getBody();
if (body != null) {
writeWithMessageConverters(body, inputMessage, outputMessage);
}
else {
outputMessage.getBody();
}
}
示例6: beforeBodyWrite
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
@Override
public BizRes beforeBodyWrite(BizRes body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response) {
String deviceToken = response.getHeaders().getFirst(requestHeaderProperties.getDeviceToken());
if (body instanceof ErrorRes) {
((ErrorRes) body).setVd(deviceToken);
return body;
}
if (body instanceof SuccessRes) {
((SuccessRes) body).setVd(deviceToken);
return body;
}
SuccessRes res = new SuccessRes(body);
res.setVd(deviceToken);
return res;
}
示例7: beforeBodyWrite
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
@Override
public Versioned beforeBodyWrite(
Versioned versioned,
MethodParameter methodParameter,
MediaType mediaType,
Class<? extends HttpMessageConverter<?>> aClass,
ServerHttpRequest serverHttpRequest,
ServerHttpResponse serverHttpResponse) {
String version = serverHttpRequest.getHeaders().getFirst(PROTOCOL_VERSION_HEADER);
if(version == null) {
throw new RuntimeException(String.format("Missing '%s' header.", PROTOCOL_VERSION_HEADER));
}
return versioned.toVersion(parseInt(version));
}
示例8: handleHttpEntityResponse
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
private void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) throws Exception {
if (responseEntity == null) {
return;
}
HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
HttpOutputMessage outputMessage = createHttpOutputMessage(webRequest);
if (responseEntity instanceof ResponseEntity && outputMessage instanceof ServerHttpResponse) {
((ServerHttpResponse) outputMessage).setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
}
HttpHeaders entityHeaders = responseEntity.getHeaders();
if (!entityHeaders.isEmpty()) {
outputMessage.getHeaders().putAll(entityHeaders);
}
Object body = responseEntity.getBody();
if (body != null) {
writeWithMessageConverters(body, inputMessage, outputMessage);
}
else {
// flush headers
outputMessage.getBody();
}
}
示例9: beforeBodyWriteInternal
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
for (String name : this.jsonpQueryParamNames) {
String value = servletRequest.getParameter(name);
if (value != null) {
if (!isValidJsonpQueryParam(value)) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring invalid jsonp parameter value: " + value);
}
continue;
}
MediaType contentTypeToUse = getContentType(contentType, request, response);
response.getHeaders().setContentType(contentTypeToUse);
bodyContainer.setJsonpFunction(value);
break;
}
}
}
示例10: handle
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
/**
* Handle a server request.
* @param request the request
* @param response the response
* @throws IOException in case of I/O errors
*/
public void handle(ServerHttpRequest request, ServerHttpResponse response)
throws IOException {
try {
Assert.state(request.getHeaders().getContentLength() > 0, "No content");
ObjectInputStream objectInputStream = new ObjectInputStream(
request.getBody());
ClassLoaderFiles files = (ClassLoaderFiles) objectInputStream.readObject();
objectInputStream.close();
this.server.updateAndRestart(files);
response.setStatusCode(HttpStatus.OK);
}
catch (Exception ex) {
logger.warn("Unable to handler restart server HTTP request", ex);
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
示例11: handledByDispatcher
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
@Test
public void handledByDispatcher() throws Exception {
HttpServletRequest request = new MockHttpServletRequest("GET", "/hello");
HttpServletResponse response = new MockHttpServletResponse();
willReturn(true).given(this.dispatcher).handle(any(ServerHttpRequest.class),
any(ServerHttpResponse.class));
this.filter.doFilter(request, response, this.chain);
verifyZeroInteractions(this.chain);
verify(this.dispatcher).handle(this.serverRequestCaptor.capture(),
this.serverResponseCaptor.capture());
ServerHttpRequest dispatcherRequest = this.serverRequestCaptor.getValue();
ServletServerHttpRequest actualRequest = (ServletServerHttpRequest) dispatcherRequest;
ServerHttpResponse dispatcherResponse = this.serverResponseCaptor.getValue();
ServletServerHttpResponse actualResponse = (ServletServerHttpResponse) dispatcherResponse;
assertThat(actualRequest.getServletRequest()).isEqualTo(request);
assertThat(actualResponse.getServletResponse()).isEqualTo(response);
}
示例12: upgrade
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
@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);
}
示例13: upgradeInternal
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
@Override
public void upgradeInternal(ServerHttpRequest httpRequest, ServerHttpResponse httpResponse,
String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint)
throws HandshakeFailureException {
HttpServletRequest request = getHttpServletRequest(httpRequest);
HttpServletResponse response = getHttpServletResponse(httpResponse);
StringBuffer requestUrl = request.getRequestURL();
String path = request.getRequestURI(); // shouldn't matter
Map<String, String> pathParams = Collections.<String, String> emptyMap();
ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
endpointConfig.setExtensions(selectedExtensions);
try {
ServerContainer container = getContainer(request);
upgradeMethod.invoke(container, request, response, endpointConfig, pathParams);
}
catch (Exception ex) {
throw new HandshakeFailureException(
"Servlet request failed to upgrade to WebSocket for " + requestUrl, ex);
}
}
示例14: applyBeforeHandshake
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
public boolean applyBeforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
Map<String, Object> attributes) throws Exception {
for (int i = 0; i < this.interceptors.size(); i++) {
HandshakeInterceptor interceptor = this.interceptors.get(i);
if (!interceptor.beforeHandshake(request, response, this.wsHandler, attributes)) {
if (logger.isDebugEnabled()) {
logger.debug(interceptor + " returns false from beforeHandshake - precluding handshake");
}
applyAfterHandshake(request, response, null);
return false;
}
this.interceptorIndex = i;
}
return true;
}
示例15: beforeBodyWrite
import org.springframework.http.server.ServerHttpResponse; //导入依赖的package包/类
@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
String etag = serverHttpRequest.getHeaders().getFirst(HttpHeaders.IF_NONE_MATCH);
if (!((GET.equals(serverHttpRequest.getMethod().name())) || (HEAD.equals(serverHttpRequest.getMethod().name())))) {
return o;
}
String md5;
if (o instanceof ResponseEntity) {
ResponseEntity response = (ResponseEntity) o;
md5 = DigestUtils.md5DigestAsHex(JsonUtils.toJsonString(response.getBody()).getBytes());
} else {
md5 = DigestUtils.md5DigestAsHex(JsonUtils.toJsonString(o).getBytes());
}
serverHttpResponse.getHeaders().add("Etag", md5);
if (md5.equalsIgnoreCase(etag)) {
serverHttpResponse.setStatusCode(HttpStatus.NOT_MODIFIED);
return null;
} else {
return o;
}
}