Java Code Examples for org.springframework.http.HttpInputMessage#getHeaders()

The following examples show how to use org.springframework.http.HttpInputMessage#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: AbstractMessageConverterMethodArgumentResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
	this.headers = inputMessage.getHeaders();
	InputStream inputStream = inputMessage.getBody();
	if (inputStream.markSupported()) {
		inputStream.mark(1);
		this.body = (inputStream.read() != -1 ? inputStream : null);
		inputStream.reset();
	}
	else {
		PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
		int b = pushbackInputStream.read();
		if (b == -1) {
			this.body = null;
		}
		else {
			this.body = pushbackInputStream;
			pushbackInputStream.unread(b);
		}
	}
}
 
Example 2
Source File: AbstractMessageConverterMethodArgumentResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
	this.headers = inputMessage.getHeaders();
	InputStream inputStream = inputMessage.getBody();
	if (inputStream.markSupported()) {
		inputStream.mark(1);
		this.body = (inputStream.read() != -1 ? inputStream : null);
		inputStream.reset();
	}
	else {
		PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
		int b = pushbackInputStream.read();
		if (b == -1) {
			this.body = null;
		}
		else {
			this.body = pushbackInputStream;
			pushbackInputStream.unread(b);
		}
	}
}
 
Example 3
Source File: AbstractMessageConverterMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
	this.headers = inputMessage.getHeaders();
	InputStream inputStream = inputMessage.getBody();
	if (inputStream == null) {
		this.body = null;
	}
	else if (inputStream.markSupported()) {
		inputStream.mark(1);
		this.body = (inputStream.read() != -1 ? inputStream : null);
		inputStream.reset();
	}
	else {
		PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
		int b = pushbackInputStream.read();
		if (b == -1) {
			this.body = null;
		}
		else {
			this.body = pushbackInputStream;
			pushbackInputStream.unread(b);
		}
	}
	this.method = ((HttpRequest) inputMessage).getMethod();
}
 
Example 4
Source File: PropertiesHttpMessageConverter.java    From SpringAll with MIT License 6 votes vote down vote up
@Override
protected Properties readInternal(Class<? extends Properties> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    Properties properties = new Properties();
    // 获取请求头
    HttpHeaders headers = inputMessage.getHeaders();
    // 获取 content-type
    MediaType contentType = headers.getContentType();
    // 获取编码
    Charset charset = null;
    if (contentType != null) {
        charset = contentType.getCharset();
    }

    charset = charset == null ? Charset.forName("UTF-8") : charset;

    // 获取请求体
    InputStream body = inputMessage.getBody();
    InputStreamReader inputStreamReader = new InputStreamReader(body, charset);

    properties.load(inputStreamReader);
    return properties;
}
 
Example 5
Source File: AbstractMessageConverterMethodArgumentResolver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
	this.headers = inputMessage.getHeaders();
	InputStream inputStream = inputMessage.getBody();
	if (inputStream == null) {
		this.body = null;
	}
	else if (inputStream.markSupported()) {
		inputStream.mark(1);
		this.body = (inputStream.read() != -1 ? inputStream : null);
		inputStream.reset();
	}
	else {
		PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
		int b = pushbackInputStream.read();
		if (b == -1) {
			this.body = null;
		}
		else {
			this.body = pushbackInputStream;
			pushbackInputStream.unread(b);
		}
	}
	this.method = ((HttpRequest) inputMessage).getMethod();
}
 
Example 6
Source File: JsonViewRequestBodyAdvice.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter,
		Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException {

	JsonView ann = methodParameter.getParameterAnnotation(JsonView.class);
	Assert.state(ann != null, "No JsonView annotation");

	Class<?>[] classes = ann.value();
	if (classes.length != 1) {
		throw new IllegalArgumentException(
				"@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter);
	}

	return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]);
}
 
Example 7
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private HttpEntity<?> resolveHttpEntityRequest(MethodParameter methodParam, NativeWebRequest webRequest)
		throws Exception {

	HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
	Class<?> paramType = getHttpEntityType(methodParam);
	Object body = readWithMessageConverters(methodParam, inputMessage, paramType);
	return new HttpEntity<Object>(body, inputMessage.getHeaders());
}
 
Example 8
Source File: JsonViewRequestBodyAdvice.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter,
		Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException {

	JsonView ann = methodParameter.getParameterAnnotation(JsonView.class);
	Assert.state(ann != null, "No JsonView annotation");

	Class<?>[] classes = ann.value();
	if (classes.length != 1) {
		throw new IllegalArgumentException(
				"@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter);
	}

	return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]);
}
 
Example 9
Source File: SecretRequestAdvice.java    From faster-framework-project with Apache License 2.0 5 votes vote down vote up
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
    String httpBody = decryptBody(inputMessage);
    if (httpBody == null) {
        throw new HttpMessageDecryptException("request body decrypt error");
    }
    return new SecretHttpMessage(new ByteArrayInputStream(httpBody.getBytes()), inputMessage.getHeaders());
}
 
Example 10
Source File: JsonViewRequestBodyAdvice.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter,
		Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException {

	JsonView annotation = methodParameter.getParameterAnnotation(JsonView.class);
	Class<?>[] classes = annotation.value();
	if (classes.length != 1) {
		throw new IllegalArgumentException(
				"@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter);
	}
	return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]);
}
 
Example 11
Source File: WxMediaResource.java    From FastBootWeixin with Apache License 2.0 5 votes vote down vote up
/**
 * 是否真的需要这么多成员变量?
 *
 * @param httpInputMessage
 * @throws IOException
 */
public WxMediaResource(HttpInputMessage httpInputMessage) throws IOException {
    this.isFileResource = false;
    if (httpInputMessage instanceof WxBufferingInputMessageWrapper) {
        this.body = ((WxBufferingInputMessageWrapper) httpInputMessage).getRawBody();
    } else {
        this.body = StreamUtils.copyToByteArray(httpInputMessage.getBody());
    }
    this.httpHeaders = httpInputMessage.getHeaders();
    this.contentType = httpHeaders.getContentType();
    this.contentLength = httpHeaders.getContentLength();
    // 判断是否是json
    if (!this.httpHeaders.containsKey(HttpHeaders.CONTENT_DISPOSITION)) {
        this.isUrlMedia = true;
        if (body[0] == '{') {
            this.url = extractURL(body);
            this.filename = extractFilenameFromURL(url);
        } else if (httpHeaders.containsKey(WxWebUtils.X_WX_REQUEST_URL)) {
            this.url = URI.create(httpHeaders.getFirst(WxWebUtils.X_WX_REQUEST_URL)).toURL();
            this.filename = extractFilenameFromURL(url);
        } else {
            this.filename = UUID.randomUUID().toString() + ".jpg";
        }
    } else {
        this.description = this.httpHeaders.getFirst(HttpHeaders.CONTENT_DISPOSITION);
        this.filename = extractFilename(this.description);
    }
}
 
Example 12
Source File: JsonViewRequestBodyAdvice.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter,
		Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException {

	JsonView annotation = methodParameter.getParameterAnnotation(JsonView.class);
	Class<?>[] classes = annotation.value();
	if (classes.length != 1) {
		throw new IllegalArgumentException(
				"@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter);
	}
	return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]);
}
 
Example 13
Source File: HandlerMethodInvoker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private HttpEntity<?> resolveHttpEntityRequest(MethodParameter methodParam, NativeWebRequest webRequest)
		throws Exception {

	HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
	Class<?> paramType = getHttpEntityType(methodParam);
	Object body = readWithMessageConverters(methodParam, inputMessage, paramType);
	return new HttpEntity<Object>(body, inputMessage.getHeaders());
}
 
Example 14
Source File: SpringManyMultipartFilesReader.java    From feign-form with Apache License 2.0 4 votes vote down vote up
@Override
protected MultipartFile[] readInternal (Class<? extends MultipartFile[]> clazz, HttpInputMessage inputMessage
) throws IOException {
  val headers = inputMessage.getHeaders();
  if (headers == null) {
    throw new HttpMessageNotReadableException("There are no headers at all.", inputMessage);
  }

  MediaType contentType = headers.getContentType();
  if (contentType == null) {
    throw new HttpMessageNotReadableException("Content-Type is missing.", inputMessage);
  }

  val boundaryBytes = getMultiPartBoundary(contentType);
  MultipartStream multipartStream = new MultipartStream(inputMessage.getBody(), boundaryBytes, bufSize, null);

  val multiparts = new LinkedList<ByteArrayMultipartFile>();
  for (boolean nextPart = multipartStream.skipPreamble(); nextPart; nextPart = multipartStream.readBoundary()) {
    ByteArrayMultipartFile multiPart;
    try {
      multiPart = readMultiPart(multipartStream);
    } catch (Exception e) {
      throw new HttpMessageNotReadableException("Multipart body could not be read.", e, inputMessage);
    }
    multiparts.add(multiPart);
  }
  return multiparts.toArray(new ByteArrayMultipartFile[0]);
}
 
Example 15
Source File: DecodeRequestBodyAdvice.java    From ueboot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public AesHttpInputMessage(HttpInputMessage inputMessage) throws Exception {
    this.headers = inputMessage.getHeaders();
    this.body = IOUtils.toInputStream(easpString(IOUtils.toString(inputMessage.getBody(), StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
}
 
Example 16
Source File: DecodeRequestBodyAdvice.java    From ueboot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public RsaHttpInputMessage(HttpInputMessage inputMessage) throws Exception {
    this.headers = inputMessage.getHeaders();
    this.body = IOUtils.toInputStream(easpString(IOUtils.toString(inputMessage.getBody(), StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
}
 
Example 17
Source File: DecodeRequestBodyAdvice.java    From ueboot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public MyHttpInputMessage(HttpInputMessage inputMessage) throws Exception {
    this.headers = inputMessage.getHeaders();
    this.body = IOUtils.toInputStream(easpString(IOUtils.toString(inputMessage.getBody(), StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
}
 
Example 18
Source File: DecryptHttpInputMessage.java    From rsa-encrypt-body-spring-boot with Apache License 2.0 4 votes vote down vote up
public DecryptHttpInputMessage(HttpInputMessage inputMessage, SecretKeyConfig secretKeyConfig, Decrypt decrypt) throws Exception {

        String privateKey =  secretKeyConfig.getPrivateKey();
        String charset = secretKeyConfig.getCharset();
        boolean showLog = secretKeyConfig.isShowLog();
        boolean timestampCheck = secretKeyConfig.isTimestampCheck();

        if (StringUtils.isEmpty(privateKey)) {
            throw new IllegalArgumentException("privateKey is null");
        }

        this.headers = inputMessage.getHeaders();
        String content = new BufferedReader(new InputStreamReader(inputMessage.getBody()))
                .lines().collect(Collectors.joining(System.lineSeparator()));
        String decryptBody;
        // 未加密内容
        if (content.startsWith("{")) {
            // 必须加密
            if (decrypt.required()) {
                log.error("not support unencrypted content:{}", content);
                throw new EncryptRequestException("not support unencrypted content");
            }
            log.info("Unencrypted without decryption:{}", content);
            decryptBody = content;
        } else {
            StringBuilder json = new StringBuilder();
            content = content.replaceAll(" ", "+");

            if (!StringUtils.isEmpty(content)) {
                String[] contents = content.split("\\|");
                for (String value : contents) {
                    value = new String(RSAUtil.decrypt(Base64Util.decode(value), privateKey), charset);
                    json.append(value);
                }
            }
            decryptBody = json.toString();
            if(showLog) {
                log.info("Encrypted data received:{},After decryption:{}", content, decryptBody);
            }
        }

        // 开启时间戳检查
        if (timestampCheck) {
            // 容忍最小请求时间
            long toleranceTime = System.currentTimeMillis() - decrypt.timeout();
            long requestTime = JsonUtils.getNode(decryptBody, "timestamp").asLong();
            // 如果请求时间小于最小容忍请求时间, 判定为超时
            if (requestTime < toleranceTime) {
                log.error("Encryption request has timed out, toleranceTime:{}, requestTime:{}, After decryption:{}", toleranceTime, requestTime, decryptBody);
                throw new EncryptRequestException("request timeout");
            }
        }

        this.body = new ByteArrayInputStream(decryptBody.getBytes());
    }
 
Example 19
Source File: ParamEncryptRequestBodyAdvice.java    From open-capacity-platform with Apache License 2.0 4 votes vote down vote up
public DefaultHttpInputMessage(HttpInputMessage inputMessage) throws Exception {
    this.headers = inputMessage.getHeaders();
    this.body = IOUtils.toInputStream(DESHelper.decrypt(this.easpString(IOUtils.toString(inputMessage.getBody(), "UTF-8"))), "UTF-8");
}