Hi
It appears you'd actually like to have JAXB based XOP support on the server
side.
So have a @MediaType(multipart/related) on the server side as well as set a
contextual property "mtom.enabled" in jaxrs:server, and here's a test
showing how to sent it all it on the client side :
@Test
public void testXopWebClient() throws Exception {
String address = "http://localhost:" + PORT + "/bookstore/xop";
JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setAddress(address);
// ensure XOP is supported
bean.setProperties(Collections.singletonMap(org.apache.cxf.message.Message.MTOM_ENABLED,
(Object)"true"));
WebClient client = bean.createWebClient();
client.type("multipart/related").accept("multipart/related");
// for debugging
HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
conduit.getClient().setReceiveTimeout(1000000);
conduit.getClient().setConnectionTimeout(1000000);
// populate your CustomObject here as needed
XopType xop = new XopType();
xop.setName("xopName");
InputStream is =
getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/book.xsd");
byte[] data = IOUtils.readBytesFromStream(is);
xop.setAttachinfo(new DataHandler(new ByteArrayDataSource(data,
"application/octet-stream")));
String bookXsd =
IOUtils.readStringFromStream(getClass().getResourceAsStream(
"/org/apache/cxf/systest/jaxrs/resources/book.xsd"));
xop.setAttachinfo2(bookXsd.getBytes());
client.post(xop, XopType.class);
}
You probably want to have InputStream instead of File...
give it a try please
Sergey
On Thu, Sep 16, 2010 at 10:25 PM, Reddy, Rama <[email protected]> wrote:
> Hi,
>
>
>
> We have a requirement in out project to create a REST service with custom
> request object and sending pdf attachment[s) as a property in that custom
> request entity. Are there any existing samples around this?
>
>
>
> As of now, I am using multipartbody to get list of attachments and
> specifically recognize my attachment from that list, is there a simple way
> to get this into a Custom request entity:
>
>
>
> Current implementation is something like this:
>
>
> public interface PDFUploadService {
> @POST
> @Path("/multipart")
> @Consumes(MediaType.MULTIPART_FORM_DATA)
> @Produces(MediaType.MULTIPART_FORM_DATA)
> public MultipartBody processMultiparts(MultipartBody multipartBody);
> }
>
>
> @WebService(endpointInterface = "com.abcd.PDFUploadService")
> public class PDFUploadServiceImpl implements PDFUploadService {
> @Override
> public MultipartBody processMultiparts(MultipartBody multipartBody) {
> if(multipartBody != null) {
> List<Attachment> attachments =
> multipartBody.getAllAttachments();
> DataHandler dataHandler1 =
> attachments.get(0).getDataHandler();
> try {
> InputStream inputStream =
> dataHandler1.getInputStream();
> File outputFile = new File("Output.pdf");
> OutputStream out=new FileOutputStream(outputFile);
> byte buf[]=new byte[1024];
> int len;
> while((len=inputStream.read(buf))>0) {
> out.write(buf,0,len);
> }
> out.close();
> } catch(Exception e) {
> }
> }
> return multipartBody;
> }
>
> }
>
>
>
> Client to invoke this is something like this:
> public class PDFUploadClient {
>
> public static void main(String[] args) throws Exception {
> HttpClient httpclient = new DefaultHttpClient();
> HttpPost httppost = new HttpPost("
> http://localhost:8080/myservice/multipart");
> File inputFile = new File("ChargersLV15.pdf");
> ContentBody inputFileBody = new FileBody(inputFile);
> MultipartEntity reqEntity = new MultipartEntity();
> reqEntity.addPart("file", inputFileBody);
> httppost.setEntity(reqEntity);
> httpclient.execute(httppost);
>
> }
>
>
>
>
>
> My requirement is something like:
> @XmlRootElement(name = "CustomRequest")
> public class CustomRequest{
>
> @XmlElement(name = "id", required = false)
> protected Long id;
> @XmlElement(name = "file", required = false)
> protected File file;
>
> }
>
>
> public interface PDFUploadService {
> @POST
> @Path("/multipart")
> @Consumes(MediaType.MULTIPART_FORM_DATA)
> public Response processMultiparts(CustomRequest customRequest);
> }
>
>
>
> How can I hook individual file to this custom request? Do I have to mention
> a different mediaType for this? Is there a best way to handle this using
> Base64 encoding?
>
>
>
>
>
> Thanks in advance,
>
> Rama
>