Hi
PDFUploadRequest has to be a JAXB bean, but besides it is probably that
you're using an older version of CXF ?
CXF 2.2.8 should have it working. One more thing to check is whether
InputStream/Filer are supported by JAXB, not quite sure, may be it is only
byte[] and DataHandler.
Oh yes, you need to have
@XmlMimeType("application/octet-stream")
attached to getters, ex, to getSeatPdfFile()
Sergey
On Wed, Sep 22, 2010 at 9:22 PM, Reddy, Rama <[email protected]>wrote:
> Hi Sergey,
>
> Thanks for a prompt response. I tried using that but I am seeing this
> exception now:
>
> Sep 22, 2010 1:08:10 PM org.apache.cxf.jaxrs.client.AbstractClient
> reportNoMessageHandler
> SEVERE: .No message body writer found for class : class
> com.custom.PDFUploadRequest.
> Sep 22, 2010 1:08:10 PM org.apache.cxf.phase.PhaseInterceptorChain
> doIntercept
> WARNING: Interceptor has thrown exception, unwinding now
> org.apache.cxf.interceptor.Fault
> at
> org.apache.cxf.jaxrs.client.WebClient$BodyWriter.handleMessage(WebClient.java:588)
> at
> org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:236)
> at
> org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:529)
> at
> org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:510)
> at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:286)
> at org.apache.cxf.jaxrs.client.WebClient.post(WebClient.java:303)
> at PDFUploadClient.main(PDFUploadClient.java:37)
> Caused by: javax.ws.rs.WebApplicationException
> at
> org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:450)
> at
> org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:365)
> at
> org.apache.cxf.jaxrs.client.WebClient$BodyWriter.handleMessage(WebClient.java:585)
> ... 6 more
> Exception in thread "main" javax.ws.rs.WebApplicationException
> at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:289)
> at org.apache.cxf.jaxrs.client.WebClient.post(WebClient.java:303)
> at PDFUploadClient.main(PDFUploadClient.java:37)
>
>
> Here is the client code:
> ------------------------------
> import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
> import org.apache.cxf.jaxrs.client.WebClient;
> import org.apache.cxf.transport.http.HTTPConduit;
>
> public class PDFUploadClient {
> public static void main(String[] args) throws Exception {
> String address = "http://localhost:8080/upload";
> 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 = (HTTPConduit)
> WebClient.getConfig(client).getConduit();
> conduit.getClient().setReceiveTimeout(1000000);
> conduit.getClient().setConnectionTimeout(1000000);
>
> File inputFile = new File("X:\\test.pdf");
> InputStream seatFile = new FileInputStream(inputFile);
>
> PDFUploadRequest uploadRequest = new PDFUploadRequest();
> uploadRequest.setTicketSeatId(12345L);
> uploadRequest.setSeatPdfFile(seatFile);
>
> client.post(uploadRequest, PDFUploadRequest.class);
> }
> }
>
> Here is my cxf-bean config:
> <bean id="pdfUploadService" class="com.service.PDFUploadServiceImpl" />
> <jaxrs:server id="pdfUploadServices" address="/">
> <jaxrs:properties>
> <entry key="mtom-enabled" value="true"/>
> </jaxrs:properties>
> <jaxrs:serviceBeans>
> <ref bean="pdfUploadService" />
> </jaxrs:serviceBeans>
> </jaxrs:server>
>
> Here is my Service interface:
> @POST
> @Path("/upload")
> @Consumes("multipart/related")
> public void uploadPDFTickets(PDFUploadRequest pdfUploadRequest);
>
> Here is my service implementation:
> public void uploadPDFTickets(PDFUploadRequest pdfUploadRequest) {
> log.info("uploadPDFTickets - pdfUploadRequest : " +
> pdfUploadRequest);
> if(pdfUploadRequest != null) {
> try {
> InputStream inputStream =
> pdfUploadRequest.getSeatPdfFile();
> String fileName =
> "Resp_"+System.currentTimeMillis()+".pdf";
> File outputFile = new
> File("X:\\nas\\rama\\transformed\\"+fileName);
> 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();
> log.info("Succesfully created a file " +
> fileName);
>
> } catch(Exception e) {
> log.error("Exception while processing the
> attachment in REST service " , e);
> }
> }
> }
>
>
> Am I missing something here?
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> -----Original Message-----
> From: Sergey Beryozkin [mailto:[email protected]]
> Sent: Friday, September 17, 2010 11:31 AM
> To: [email protected]
> Subject: Re: Looking for samples on sending binary files in a REST service
> using Base64 encoding..
>
> 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
> >
>