Hi Sergey,

I somehow figured that JAXB bindings are missing in my CustomRequest object but 
now I am ending up in following issue. It is not able to create a instance of 
InputStream. I chose wild card type, so that everything will be accepted? Will 
that cause any issue?

Encoding: ISO-8859-1
Content-Type: */*
Headers: {content-type=[*/*], connection=[keep-alive], host=[ilog.srwd00.com], 
content-length=[59], user-agent=[Apache CXF 2.2.3], Content-Type=[*/*], 
Accept=[*/*], pragma=[no-cache], Cache-Contr
ol=[no-cache]}
Payload: {"PDFUploadRequest":{"ticketSeatId":12345,"fileStream":""}}
--------------------------------------
2010-09-22 16:00:09,645 ERROR [STDERR] Sep 22, 2010 4:00:09 PM 
org.apache.cxf.jaxrs.provider.AbstractJAXBProvider handleJAXBException
WARNING: javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.bind.UnmarshalException: Unable to create an instance of 
java.io.InputStream
 - with linked exception:
[java.lang.InstantiationException]]
        at 
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:421)
        at 
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360)
        at 
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:330)
        at 
org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:156)
        at 
org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:861)
        at 
org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:494)
        at 
org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:459)
        at 
org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:204)
        at 
org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:65)
        at 
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:236)
        at 
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:104)
        at 
org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:99)
        at 
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:452)
        at 
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:159)
        at 
org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:220)
        at 
org.apache.cxf.transport.servlet.AbstractCXFServlet.doPost(AbstractCXFServlet.java:153)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
        at 
org.apache.cxf.transport.servlet.AbstractCXFServlet.service(AbstractCXFServlet.java:211)
        at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at 
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
        at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
        at 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at 
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
        at 
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
        at 
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at 
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
        at 
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
        at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:437)
        at 
org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:366)
        at 
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
        at java.lang.Thread.run(Thread.java:619)
Caused by: javax.xml.bind.UnmarshalException: Unable to create an instance of 
java.io.InputStream
 - with linked exception:

Am I missing something here.. 

Thanks
Rama

-----Original Message-----
From: Reddy, Rama 
Sent: Wednesday, September 22, 2010 1:23 PM
To: [email protected]; '[email protected]'
Cc: Pydipati, Karuna
Subject: RE: Looking for samples on sending binary files in a REST service 
using Base64 encoding..

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
>

Reply via email to