use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JAXRSMultipartTest method testLargerThanDefaultHeader.
// The Content Disposition header will be accepted here, even though it is larger than the default,
// as we have configured a larger value on the service side
@Test
public void testLargerThanDefaultHeader() throws Exception {
InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
String address = "http://localhost:" + PORT + "/bookstore/books/image";
WebClient client = WebClient.create(address);
client.type("multipart/mixed").accept("multipart/mixed");
WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true");
StringBuilder sb = new StringBuilder();
sb.append("form-data;");
for (int i = 0; i < 35; i++) {
sb.append("aaaaaaaaaa");
}
MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
headers.putSingle("Content-ID", "root");
headers.putSingle("Content-Type", "application/octet-stream");
headers.putSingle("Content-Disposition", sb.toString());
DataHandler handler = new DataHandler(new InputStreamDataSource(is1, "application/octet-stream"));
Attachment att = new Attachment(headers, handler, null);
Response response = client.post(att);
assertEquals(response.getStatus(), 200);
client.close();
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MessageContextImpl method createAttachments.
private MultipartBody createAttachments(String propertyName) {
Message inMessage = m.getExchange().getInMessage();
boolean embeddedAttachment = inMessage.get("org.apache.cxf.multipart.embedded") != null;
Object o = inMessage.get(propertyName);
if (o != null) {
return (MultipartBody) o;
}
if (embeddedAttachment) {
inMessage = new MessageImpl();
inMessage.setExchange(new ExchangeImpl());
inMessage.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_DIRECTORY));
inMessage.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD));
inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_SIZE));
inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE));
inMessage.setContent(InputStream.class, m.getExchange().getInMessage().get("org.apache.cxf.multipart.embedded.input"));
inMessage.put(Message.CONTENT_TYPE, m.getExchange().getInMessage().get("org.apache.cxf.multipart.embedded.ctype").toString());
}
new AttachmentInputInterceptor().handleMessage(inMessage);
List<Attachment> newAttachments = new LinkedList<Attachment>();
try {
Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) inMessage.get(AttachmentDeserializer.ATTACHMENT_PART_HEADERS));
Attachment first = new Attachment(AttachmentUtil.createAttachment(inMessage.getContent(InputStream.class), headers), new ProvidersImpl(inMessage));
newAttachments.add(first);
} catch (IOException ex) {
throw ExceptionUtils.toInternalServerErrorException(ex, null);
}
Collection<org.apache.cxf.message.Attachment> childAttachments = inMessage.getAttachments();
if (childAttachments == null) {
childAttachments = Collections.emptyList();
}
childAttachments.size();
for (org.apache.cxf.message.Attachment a : childAttachments) {
newAttachments.add(new Attachment(a, new ProvidersImpl(inMessage)));
}
MediaType mt = embeddedAttachment ? (MediaType) inMessage.get("org.apache.cxf.multipart.embedded.ctype") : getHttpHeaders().getMediaType();
MultipartBody body = new MultipartBody(newAttachments, mt, false);
inMessage.put(propertyName, body);
return body;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MultipartProvider method getAttachments.
private List<Attachment> getAttachments(List<?> objects, String rootMediaType) throws IOException {
List<Attachment> handlers = new ArrayList<>(objects.size());
for (int i = 0; i < objects.size(); i++) {
Object value = objects.get(i);
Attachment handler = createDataHandler(value, value.getClass(), new Annotation[] {}, rootMediaType, rootMediaType, i);
handlers.add(handler);
}
return handlers;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MultipartProvider method getAttachmentCollection.
private Object getAttachmentCollection(Type t, List<Attachment> infos, Annotation[] anns) throws IOException {
Class<?> actual = getActualType(t, 0);
if (Attachment.class.isAssignableFrom(actual)) {
return infos;
}
Collection<Object> objects = new ArrayList<>();
for (Attachment a : infos) {
objects.add(fromAttachment(a, actual, actual, anns));
}
return objects;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MultipartProvider method convertToDataHandlers.
private List<Attachment> convertToDataHandlers(Object obj, Class<?> type, Type genericType, Annotation[] anns, MediaType mt) throws IOException {
if (Map.class.isAssignableFrom(obj.getClass())) {
Map<Object, Object> objects = CastUtils.cast((Map<?, ?>) obj);
List<Attachment> handlers = new ArrayList<>(objects.size());
int i = 0;
for (Iterator<Map.Entry<Object, Object>> iter = objects.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<Object, Object> entry = iter.next();
Object value = entry.getValue();
Attachment handler = createDataHandler(value, value.getClass(), new Annotation[] {}, entry.getKey().toString(), mt.toString(), i++);
handlers.add(handler);
}
return handlers;
}
String rootMediaType = getRootMediaType(anns, mt);
if (List.class.isAssignableFrom(obj.getClass())) {
return getAttachments((List<?>) obj, rootMediaType);
}
if (MultipartBody.class.isAssignableFrom(type)) {
List<Attachment> atts = ((MultipartBody) obj).getAllAttachments();
// these attachments may have no DataHandlers, but objects only
return getAttachments(atts, rootMediaType);
}
Attachment handler = createDataHandler(obj, genericType, anns, rootMediaType, mt.toString(), 1);
return Collections.singletonList(handler);
}
Aggregations