本文整理汇总了Java中io.netty.handler.codec.http.multipart.InterfaceHttpData类的典型用法代码示例。如果您正苦于以下问题:Java InterfaceHttpData类的具体用法?Java InterfaceHttpData怎么用?Java InterfaceHttpData使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InterfaceHttpData类属于io.netty.handler.codec.http.multipart包,在下文中一共展示了InterfaceHttpData类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPostParameter
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
public static String getPostParameter(HttpRequest req, String name) {
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(
new DefaultHttpDataFactory(false), req);
InterfaceHttpData data = decoder.getBodyHttpData(name);
if (data.getHttpDataType() == HttpDataType.Attribute) {
Attribute attribute = (Attribute) data;
String value = null;
try {
value = attribute.getValue();
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
return null;
}
示例2: parsePostParam
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
private void parsePostParam(InterfaceHttpData data) {
try {
switch (data.getHttpDataType()) {
case Attribute:
Attribute param = (Attribute) data;
this.paramsMap.put(param.getName(), Collections.singletonList(param.getValue()));
break;
default:
break;
}
} catch (IOException e) {
e.printStackTrace();
}finally {
data.release();
}
}
示例3: requestParametersHandler
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
/**
* request parameters put in {@link #parameters}
*
* @param req
*/
private void requestParametersHandler(HttpRequest req) {
if (req.method().equals(HttpMethod.POST)) {
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(req);
try {
List<InterfaceHttpData> postList = decoder.getBodyHttpDatas();
for (InterfaceHttpData data : postList) {
List<String> values = new ArrayList<String>();
MixedAttribute value = (MixedAttribute) data;
value.setCharset(CharsetUtil.UTF_8);
values.add(value.getValue());
this.parameters.put(data.getName(), values);
}
} catch (Exception e) {
logger.error(e.getMessage());
}
}
}
示例4: getMultipartParts
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public synchronized List<InterfaceHttpData> getMultipartParts() {
if (!isMultipartRequest() || !isCompleteRequestWithAllChunks())
return null;
if (multipartData == null) {
byte[] contentBytes = getRawContentBytes();
HttpRequest fullHttpRequestForMultipartDecoder =
(contentBytes == null)
? new DefaultFullHttpRequest(getProtocolVersion(), getMethod(), getUri())
: new DefaultFullHttpRequest(getProtocolVersion(), getMethod(), getUri(),
Unpooled.wrappedBuffer(contentBytes));
fullHttpRequestForMultipartDecoder.headers().add(getHeaders());
multipartData = new HttpPostMultipartRequestDecoder(
new DefaultHttpDataFactory(false), fullHttpRequestForMultipartDecoder, getContentCharset()
);
}
return multipartData.getBodyHttpDatas();
}
示例5: getMultipartParts_works_as_expected_with_known_valid_data
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
@Test
public void getMultipartParts_works_as_expected_with_known_valid_data() throws IOException {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
Whitebox.setInternalState(requestInfo, "isMultipart", true);
Whitebox.setInternalState(requestInfo, "contentCharset", CharsetUtil.UTF_8);
Whitebox.setInternalState(requestInfo, "protocolVersion", HttpVersion.HTTP_1_1);
Whitebox.setInternalState(requestInfo, "method", HttpMethod.POST);
requestInfo.isCompleteRequestWithAllChunks = true;
requestInfo.rawContentBytes = KNOWN_MULTIPART_DATA_BODY.getBytes(CharsetUtil.UTF_8);
requestInfo.getHeaders().set("Content-Type", KNOWN_MULTIPART_DATA_CONTENT_TYPE_HEADER);
// when
List<InterfaceHttpData> result = requestInfo.getMultipartParts();
// then
assertThat(result, notNullValue());
assertThat(result.size(), is(1));
InterfaceHttpData data = result.get(0);
assertThat(data, instanceOf(FileUpload.class));
FileUpload fileUploadData = (FileUpload)data;
assertThat(fileUploadData.getName(), is(KNOWN_MULTIPART_DATA_NAME));
assertThat(fileUploadData.getFilename(), is(KNOWN_MULTIPART_DATA_FILENAME));
assertThat(fileUploadData.getString(CharsetUtil.UTF_8), is(KNOWN_MULTIPART_DATA_ATTR_UUID));
}
示例6: getMultipartParts_works_as_expected_with_known_empty_data
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
@Test
public void getMultipartParts_works_as_expected_with_known_empty_data() throws IOException {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
Whitebox.setInternalState(requestInfo, "isMultipart", true);
Whitebox.setInternalState(requestInfo, "contentCharset", CharsetUtil.UTF_8);
Whitebox.setInternalState(requestInfo, "protocolVersion", HttpVersion.HTTP_1_1);
Whitebox.setInternalState(requestInfo, "method", HttpMethod.POST);
requestInfo.isCompleteRequestWithAllChunks = true;
requestInfo.rawContentBytes = null;
requestInfo.getHeaders().set("Content-Type", KNOWN_MULTIPART_DATA_CONTENT_TYPE_HEADER);
// when
List<InterfaceHttpData> result = requestInfo.getMultipartParts();
// then
assertThat(result, notNullValue());
assertThat(result.isEmpty(), is(true));
}
示例7: getMultipartParts_explodes_if_multipartData_had_been_released
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
@Test(expected = IllegalStateException.class)
public void getMultipartParts_explodes_if_multipartData_had_been_released() throws IOException {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
Whitebox.setInternalState(requestInfo, "isMultipart", true);
Whitebox.setInternalState(requestInfo, "contentCharset", CharsetUtil.UTF_8);
Whitebox.setInternalState(requestInfo, "protocolVersion", HttpVersion.HTTP_1_1);
Whitebox.setInternalState(requestInfo, "method", HttpMethod.POST);
requestInfo.isCompleteRequestWithAllChunks = true;
requestInfo.rawContentBytes = KNOWN_MULTIPART_DATA_BODY.getBytes(CharsetUtil.UTF_8);
requestInfo.getHeaders().set("Content-Type", KNOWN_MULTIPART_DATA_CONTENT_TYPE_HEADER);
List<InterfaceHttpData> result = requestInfo.getMultipartParts();
assertThat(result, notNullValue());
assertThat(result.size(), is(1));
// expect
requestInfo.releaseMultipartData();
requestInfo.getMultipartParts();
fail("Expected an error, but none was thrown");
}
示例8: getMultipartParts_returns_data_from_multipartData
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
@Test
public void getMultipartParts_returns_data_from_multipartData() {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
Whitebox.setInternalState(requestInfo, "isMultipart", true);
requestInfo.isCompleteRequestWithAllChunks = true;
HttpPostMultipartRequestDecoder multipartDataMock = mock(HttpPostMultipartRequestDecoder.class);
List<InterfaceHttpData> dataListMock = mock(List.class);
doReturn(dataListMock).when(multipartDataMock).getBodyHttpDatas();
requestInfo.multipartData = multipartDataMock;
// when
List<InterfaceHttpData> result = requestInfo.getMultipartParts();
// then
assertThat(result, is(dataListMock));
}
示例9: handleUploadMessage
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
private void handleUploadMessage(HttpMessage httpMsg, Message uploadMessage) throws IOException{
if (httpMsg instanceof HttpContent) {
HttpContent chunk = (HttpContent) httpMsg;
decoder.offer(chunk);
try {
while (decoder.hasNext()) {
InterfaceHttpData data = decoder.next();
if (data != null) {
try {
handleUploadFile(data, uploadMessage);
} finally {
data.release();
}
}
}
} catch (EndOfDataDecoderException e1) {
//ignore
}
if (chunk instanceof LastHttpContent) {
resetUpload();
}
}
}
示例10: copyHttpBodyData
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
void copyHttpBodyData(FullHttpRequest fullHttpReq, MockHttpServletRequest servletRequest){
ByteBuf bbContent = fullHttpReq.content();
if(bbContent.hasArray()) {
servletRequest.setContent(bbContent.array());
} else {
if(fullHttpReq.getMethod().equals(HttpMethod.POST)){
HttpPostRequestDecoder decoderPostData = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), fullHttpReq);
String bbContentStr = bbContent.toString(Charset.forName(UTF_8));
servletRequest.setContent(bbContentStr.getBytes());
if( ! decoderPostData.isMultipart() ){
List<InterfaceHttpData> postDatas = decoderPostData.getBodyHttpDatas();
for (InterfaceHttpData postData : postDatas) {
if (postData.getHttpDataType() == HttpDataType.Attribute) {
Attribute attribute = (Attribute) postData;
try {
servletRequest.addParameter(attribute.getName(),attribute.getValue());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
示例11: readHttpDataChunkByChunk
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
private void readHttpDataChunkByChunk() throws IOException
{
while (decoder.hasNext())
{
InterfaceHttpData data = decoder.next();
if (data != null)
{
try
{
// new value
writeHttpData(data);
}
finally
{
data.release();
}
}
}
}
示例12: writeHttpData
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
private void writeHttpData(InterfaceHttpData data) throws IOException
{
if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute)
{
Attribute attribute = (Attribute) data;
String value = attribute.getValue();
if (value.length() > 65535)
{
throw new IOException("Data too long");
}
req.addPostParameter(attribute.getName(), value);
}
else
{
if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.FileUpload)
{
FileUpload fileUpload = (FileUpload) data;
req.addFileUpload(fileUpload);
}
}
}
示例13: readHttpDataChunkByChunk
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
/**
* Example of reading request by chunk and getting values from chunk to chunk
*/
private void readHttpDataChunkByChunk() {
try {
while (decoder.hasNext()) {
InterfaceHttpData data = decoder.next();
if (data != null) {
try {
// new value
writeHttpData(data);
} finally {
data.release();
}
}
}
} catch (EndOfDataDecoderException e1) {
// end
responseContent.append("\r\n\r\nEND OF CONTENT CHUNK BY CHUNK\r\n\r\n");
}
}
示例14: decodePost
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
private void decodePost() {
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(request);
// Form Data
List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas();
try {
for (InterfaceHttpData data : bodyHttpDatas) {
if (data.getHttpDataType().equals(HttpDataType.Attribute)) {
Attribute attr = (MixedAttribute) data;
params.put(data.getName(),
new Param(Arrays.asList(attr.getValue())));
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
decoder.destroy();
}
示例15: get
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入依赖的package包/类
@Override
public Object get(ChannelHandlerContext ctx, URIDecoder uriDecoder) {
List<InterfaceHttpData> bodyHttpDatas = uriDecoder.getBodyHttpDatas();
if (bodyHttpDatas == null || bodyHttpDatas.size() == 0) {
return null;
}
for (InterfaceHttpData data : bodyHttpDatas) {
if (name.equals(data.getName())) {
if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
Attribute attribute = (Attribute) data;
try {
return ReflectionUtil.castTo(type, attribute.getValue());
} catch (IOException e) {
log.error("Error getting form params. Reason : {}", e.getMessage(), e);
}
}
}
}
return null;
}