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

The following examples show how to use org.springframework.http.HttpInputMessage#getBody() . 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: AbstractWireFeedHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	WireFeedInput feedInput = new WireFeedInput();
	MediaType contentType = inputMessage.getHeaders().getContentType();
	Charset charset = (contentType != null && contentType.getCharset() != null ?
			contentType.getCharset() : DEFAULT_CHARSET);
	try {
		Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
		return (T) feedInput.build(reader);
	}
	catch (FeedException ex) {
		throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex, inputMessage);
	}
}
 
Example 2
Source File: SourceHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	InputStream body = inputMessage.getBody();
	if (DOMSource.class == clazz) {
		return (T) readDOMSource(body);
	}
	else if (SAXSource.class == clazz) {
		return (T) readSAXSource(body);
	}
	else if (StAXSource.class == clazz) {
		return (T) readStAXSource(body);
	}
	else if (StreamSource.class == clazz || Source.class == clazz) {
		return (T) readStreamSource(body);
	}
	else {
		throw new HttpMessageConversionException("Could not read class [" + clazz +
				"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.");
	}
}
 
Example 3
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 4
Source File: AbstractWireFeedHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	WireFeedInput feedInput = new WireFeedInput();
	MediaType contentType = inputMessage.getHeaders().getContentType();
	Charset charset = (contentType != null && contentType.getCharset() != null ?
			contentType.getCharset() : DEFAULT_CHARSET);
	try {
		Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
		return (T) feedInput.build(reader);
	}
	catch (FeedException ex) {
		throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex);
	}
}
 
Example 5
Source File: AbstractWireFeedHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	WireFeedInput feedInput = new WireFeedInput();
	MediaType contentType = inputMessage.getHeaders().getContentType();
	Charset charset = (contentType != null && contentType.getCharset() != null ?
			contentType.getCharset() : DEFAULT_CHARSET);
	try {
		Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
		return (T) feedInput.build(reader);
	}
	catch (FeedException ex) {
		throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex, inputMessage);
	}
}
 
Example 6
Source File: SourceHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	InputStream body = inputMessage.getBody();
	if (DOMSource.class == clazz) {
		return (T) readDOMSource(body, inputMessage);
	}
	else if (SAXSource.class == clazz) {
		return (T) readSAXSource(body, inputMessage);
	}
	else if (StAXSource.class == clazz) {
		return (T) readStAXSource(body, inputMessage);
	}
	else if (StreamSource.class == clazz || Source.class == clazz) {
		return (T) readStreamSource(body);
	}
	else {
		throw new HttpMessageNotReadableException("Could not read class [" + clazz +
				"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.", inputMessage);
	}
}
 
Example 7
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 8
Source File: YahooQuoteMessageConverter.java    From cloudstreetmarket.com with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected QuoteWrapper readInternal(Class<? extends QuoteWrapper> clazz, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
    CSVReader reader = new CSVReader(new InputStreamReader(httpInputMessage.getBody()));
    List<String[]> rows = reader.readAll();
    QuoteWrapper quoteWrapper = new QuoteWrapper();
    for (String[] row : rows) {
    	quoteWrapper.add(new YahooQuote(row[0], 
    								row[1], 
    								parseDouble(row[2]), 
    								parseDouble(row[3]), 
    								parseDouble(row[4]), 
    								parseDouble(row[5]), 
    								parsePercent(row[6]), 
    								parseDouble(row[7]), 
    								parseDouble(row[8]), 
    								parseDouble(row[9]), 
    								parseDouble(row[10]), 
    								parseInt(row[11]), 
    								row[12], 
    								row[13]));
    }

    return quoteWrapper;
}
 
Example 9
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 10
Source File: Fastjson2HttpMessageConverter.java    From dpCms with Apache License 2.0 6 votes vote down vote up
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException,  HttpMessageNotReadableException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream in = inputMessage.getBody();
    byte[] buf = new byte[1024];
    for (;;) {
        int len = in.read(buf);
        if (len == -1) {
            break;
        }
        if (len > 0) {
            baos.write(buf, 0, len);
        }
    }
    byte[] bytes = baos.toByteArray();
    return JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);
}
 
Example 11
Source File: VvFileHttpMessageConverter.java    From moVirt with Apache License 2.0 5 votes vote down vote up
@Override
protected Object readInternal(Class<? extends Object> clazz,
                              HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    Object result = null;
    InputStream inputStream = inputMessage.getBody();
    try {
        result = convertStreamToConsoleConnectionDetails(inputStream);
    } catch (Exception x) {
        throw new IllegalStateException("Couldn't parse .vv file response", x);
    } finally {
        ObjectUtils.closeSilently(inputStream);
    }

    return result;
}
 
Example 12
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 13
Source File: WithSignMessageConverter.java    From MeetingFilm with Apache License 2.0 5 votes vote down vote up
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {

    InputStream in = inputMessage.getBody();
    Object o = JSON.parseObject(in, super.getFastJsonConfig().getCharset(), BaseTransferEntity.class, super.getFastJsonConfig().getFeatures());

    //先转化成原始的对象
    BaseTransferEntity baseTransferEntity = (BaseTransferEntity) o;

    //校验签名
    String token = HttpKit.getRequest().getHeader(jwtProperties.getHeader()).substring(7);
    String md5KeyFromToken = jwtTokenUtil.getMd5KeyFromToken(token);

    String object = baseTransferEntity.getObject();
    String json = dataSecurityAction.unlock(object);
    String encrypt = MD5Util.encrypt(object + md5KeyFromToken);

    if (encrypt.equals(baseTransferEntity.getSign())) {
        System.out.println("签名校验成功!");
    } else {
        System.out.println("签名校验失败,数据被改动过!");
        throw new GunsException(BizExceptionEnum.SIGN_ERROR);
    }

    //校验签名后再转化成应该的对象
    return JSON.parseObject(json, type);
}
 
Example 14
Source File: FastJsonHttpMessageConverter.java    From mcg-helper with Apache License 2.0 5 votes vote down vote up
@Override
public Object read(Type type, Class<?> contextClass,
        HttpInputMessage inputMessage) throws IOException,
        HttpMessageNotReadableException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    InputStream in = inputMessage.getBody();

    byte[] buf = new byte[1024];
    for (;;) {
        int len = in.read(buf);
        if (len == -1) {
            break;
        }

        if (len > 0) {
            baos.write(buf, 0, len);
        }
    }

    byte[] bytes = baos.toByteArray();
    this.readBefore(bytes);
    Object obj = JSON.parseObject(bytes, 0, bytes.length,
            charset.newDecoder(), type);
    this.readAfter(obj);
    return obj;
}
 
Example 15
Source File: WithSignMessageConverter.java    From MeetingFilm with Apache License 2.0 5 votes vote down vote up
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {

    InputStream in = inputMessage.getBody();
    Object o = JSON.parseObject(in, super.getFastJsonConfig().getCharset(), BaseTransferEntity.class, super.getFastJsonConfig().getFeatures());

    //先转化成原始的对象
    BaseTransferEntity baseTransferEntity = (BaseTransferEntity) o;

    //校验签名
    String token = HttpKit.getRequest().getHeader(jwtProperties.getHeader()).substring(7);
    String md5KeyFromToken = jwtTokenUtil.getMd5KeyFromToken(token);

    String object = baseTransferEntity.getObject();
    String json = dataSecurityAction.unlock(object);
    String encrypt = MD5Util.encrypt(object + md5KeyFromToken);

    if (encrypt.equals(baseTransferEntity.getSign())) {
        System.out.println("签名校验成功!");
    } else {
        System.out.println("签名校验失败,数据被改动过!");
        throw new GunsException(BizExceptionEnum.SIGN_ERROR);
    }

    //校验签名后再转化成应该的对象
    return JSON.parseObject(json, type);
}
 
Example 16
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 17
Source File: AbstractMessageConverter.java    From elucidate-server with MIT License 4 votes vote down vote up
private String consume(HttpInputMessage inputMessage) throws IOException {
    InputStream messageInputStream = inputMessage.getBody();
    return IOUtils.toString(messageInputStream, Charset.defaultCharset());
}
 
Example 18
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 19
Source File: Log4jFastJsonHttpMessageConverter.java    From ueboot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException,
        HttpMessageNotReadableException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream in = inputMessage.getBody();
    byte[] buf = new byte[1024];
    for (; ; ) {
        int len = in.read(buf);
        if (len == -1) {
            break;
        }
        if (len > 0) {
            baos.write(buf, 0, len);
        }
    }

    byte[] bytes = baos.toByteArray();
    String jsonStr = new String(bytes);
    //防止xss攻击,sql注入
    XSSNotCheck notCheck = clazz.getAnnotation(XSSNotCheck.class);
    //加了注解则不仅限xss字段拦截
    if(notCheck==null){
        jsonStr = XSSUtil.checkXssStr(jsonStr);
    }else{
        log.warn("当前类:{}标注无需进行xss字段拦截,注意安全!",clazz.getName());
    }
    bytes = jsonStr.getBytes();
    NotLog notLog = clazz.getAnnotation(NotLog.class);
    if(notLog==null){
        log.info("Request Class:{},json:{}", clazz.getName(), jsonStr);
    }

    Object reqBody = JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);

    Set<ConstraintViolation<Object>> validRetval = this.getValidator().validate(reqBody);
    //自定义处理校验结论
    if(!httpRequestValidatorService.doValidatorMsg(validRetval)){
        StringBuilder sb = new StringBuilder();
        // 校验失败
        if (!validRetval.isEmpty()) {
            for (ConstraintViolation<Object> t : validRetval) {
                sb.append(t.getPropertyPath()).append(t.getMessage()).append(",");
            }
        }
        String checkError = sb.toString();
        if (!isEmpty(checkError)) {
            checkError = "请求参数格式校验不通过:" + checkError;
            throw new BusinessException(checkError);
        }
    }
    httpRequestValidatorService.validator(jsonStr,clazz);

    return reqBody;
}
 
Example 20
Source File: KryoHttpMessageConverter.java    From tutorials with MIT License 4 votes vote down vote up
@Override
protected Object readInternal(final Class<? extends Object> clazz,
		final HttpInputMessage inputMessage) throws IOException {
	final Input input = new Input(inputMessage.getBody());
	return kryoThreadLocal.get().readClassAndObject(input);
}