Hi,

As you see in following code, My requirement is to send one or more XML
files in response stream. I have done following to accomplish my
requirement:

1. Resource class method returns ArrayList which contains XML file objects.
2. I have created custom MessageBodyWriter, which modify the response, by
getting files from Arraylist and write it into Response Stream.

I am accessing my service from Browser with url :
http://localhost:443/application/service?x=fileDir

I have checked that I have got all the files in response body with 200Ok
code, but browser shows XML parsing error as shown below.

"XML Parsing Error: junk after document element"

so is it problem from CXF side or it is browser error? any idea? Please note
that if I send single xml file with same code, it returns successfully and
browser display that file properly. I guess that because there are multiple
files one after another, browser is not able to parsing it? is that true?

Please note, I need to send multiple files in response stream as it is third
party client requirement.


Regards,
Parimal


On Tue, Dec 1, 2009 at 2:09 PM, Parimal Dhinoja <[email protected]> wrote:

> Hi,
>
> I am getting error that my custom MessageBodyWriter  is not found.
> following is my code. Please let me know what went wrong.
>
> My customMessageBodyWriter class
>
> @Provider
> @Produces("text/xml")
> public class CustomResBodyWriter implements
> MessageBodyWriter<ArrayList<Object>>
> {
>     public static final int ARRAY_SIZE = 5000;
>     private static final Logger LOGGER =
> Logger.getLogger(CustomResBodyWriter.class);
>
>     @Override
>     public long getSize(ArrayList<Object> arg0, Class<?> arg1,
> java.lang.reflect.Type arg2,
>                         Annotation[] arg3, MediaType arg4)
>     {
>          return arg0.size();
>     }
>
>     @Override
>     public boolean isWriteable(Class<?> arg0, java.lang.reflect.Type arg1,
> Annotation[] arg2,
>                                MediaType arg3)
>     {
>            return ArrayList.class.isAssignableFrom(arg0);
>     }
>
>     @Override
>     public void writeTo(ArrayList<Object> arg0, Class<?> arg1,
> java.lang.reflect.Type arg2,
>                         Annotation[] arg3, MediaType arg4,
> MultivaluedMap<String, Object> arg5,
>                         OutputStream arg6) throws IOException,
> WebApplicationException
>     {
>         BufferedWriter bw = new BufferedWriter(new
> OutputStreamWriter(arg6));
>         byte[] buffer = new byte[ARRAY_SIZE];
>         String ts = null;
>         int listSize = arg0.size();
>         FileInputStream fis = null;
>         for (int i = 0; i < listSize; i++)
>         {
>             File vFile = (File) arg0.get(i);
>             fis = new FileInputStream(vFile);
>             BufferedInputStream bufferedInputStream = new
> BufferedInputStream(fis);
>             while (true)
>             {
>                 int vBytesRead = bufferedInputStream.read(buffer, 0,
> buffer.length);
>                 if (vBytesRead < 0)
>                 {
>                     break;
>                 }
>                 arg6.write(buffer, 0, vBytesRead);
>             }
>             fis.close();
>             bufferedInputStream.close();
>             vFile = null;
>         }
>         arg6.flush();
>
>     }
> }
>
> my configuration.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans";
>   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>   xmlns:jaxrs="http://cxf.apache.org/jaxrs";
>   xmlns:sec="http://cxf.apache.org/configuration/security";
>   xmlns:http="http://cxf.apache.org/transports/http/configuration";
>   xmlns:cxf="http://cxf.apache.org/core";
>   xsi:schemaLocation="
> http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans.xsd
> http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
> http://cxf.apache.org/transports/http/configuration
> http://cxf.apache.org/schemas/configuration/http-conf.xsd
> http://cxf.apache.org/configuration/security
> http://cxf.apache.org/schemas/configuration/security.xsd
> http://cxf.apache.org/core
> http://cxf.apache.org/schemas/core.xsd";>
>
>   <!-- do not use import statements if CXFServlet init parameters link to
> this beans.xml -->
>
>   <import resource="classpath:META-INF/cxf/cxf.xml" />
>   <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"
> />
>   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
>
>  <bean id="mapProvider"
> class="com.traveltripper.stargazer.service.pms.opera.CustomResBodyWriter"/>
>
>   <jaxrs:server id="messageservice" address="/">
> <jaxrs:features>
>      <cxf:logging/>
> </jaxrs:features>
>     <jaxrs:serviceBeans>
>       <ref bean="messageBean" />
>     </jaxrs:serviceBeans>
>      <jaxrs:providers>
>       <ref bean="mapProvider" />
>      </jaxrs:providers>
>   </jaxrs:server>
>
>   <bean id="messageBean"
> class="com.traveltripper.stargazer.service.pms.opera.ReservationXMLService"
> />
> </beans>
>
> my service implementation.
>
>  @GET
>     @Path("/message5")
>     @Produces("application/xml")
>     public ArrayList<Object> getMessage5(@QueryParam("propertyName")
>     String propertyName)
>     {
>         ArrayList<Object> fileList = new ArrayList<Object>();
>         try
>         {
>             LOGGER.info("propertyName in Request :" + propertyName);
>
>             String filePath = "c://";
>             LOGGER.info("FilePath=" + filePath);
>             File vDirpath = new File(filePath);
>             ArrayList<String> vProperties = new ArrayList<String>();
>
>             String dirname[] = vDirpath.list();
>             for (int i = 0; i < dirname.length; i++)
>             {
>                 vProperties.add(dirname[i]);
>                 LOGGER.info(dirname[i]);
>             }
>
>             if (vProperties.contains(propertyName))
>             {
>
>                 filePath = filePath + propertyName + "/";
>                 File propertyPath = new File(filePath);
>                 File[] resFiles = propertyPath.listFiles();
>
>                 for (int i = 0; i < resFiles.length; i++)
>                 {
>                     fileList.add(resFiles[i]);
>
>                 }
>
>             }
>         }
>         catch (Exception e)
>         {
>             LOGGER.info(e.getMessage());
>         }
>         return fileList;
>
>     }
>
>
> --
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
>



-- 
Regards,
Parimal
"Nothing is stationary,Change is a part of Life"

Reply via email to