You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wink.apache.org by "അജിയോസ്‌ യോഹന്നാന്‍ (Ajiyos)" <aj...@gmail.com> on 2009/11/02 21:03:21 UTC

Fwd: Question on JAXB ID resolver

I didnt get any response in user group. Trying my luck here.

---------- Forwarded message ----------
From: അജിയോസ്‌ യോഹന്നാന്‍(Ajiyos) <aj...@gmail.com>
Date: 2009/11/2
Subject: Question on JAXB ID resolver
To: wink-user@incubator.apache.org


Hi,

Can any one tell me how to hookup my custom IDResolver to wink framework.

The IDResolver need to be configured as a property to my jaxb unmarshaller.
However I coudnt find out how to get handle to unmarshaller.

Thanks,
Ajiyos

-- 
"Attitude not the Aptitude determines the Altitude"



-- 
"Attitude not the Aptitude determines the Altitude"

Re: Question on JAXB ID resolver

Posted by "അജിയോസ്‌ യോഹന്നാന്‍ (Ajiyos)" <aj...@gmail.com>.
Bryant,

Thanks for the solution. I have opened a JIRA WINK-224.

On the meantime I will try to create a provider.

Thanks,
Ajiyos

On Tue, Nov 3, 2009 at 1:53 AM, Bryant Luk <br...@gmail.com> wrote:

> Hi Ajiyos,
>
> You will need to write your own entity provider in this case (although
> you can set properties on the marshalling side, the unmarshalling does
> not currently).  If you want this feature, please open a JIRA against
> Apache Wink (https://issues.apache.org/jira/browse/WINK) for future
> consideration as time permits.
>
> If you want a solution immediately, you can look at the source for our
> JAXB XML providers here and base your solution on them:
>
> http://svn.apache.org/repos/asf/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/
>
> For instance, you could write something like:
>
> @Provider
> public MyXmlProvider implements MessageBodyReader<Object> {
>
>    public boolean isReadable(Class<?> type,
>                              Type genericType,
>                              Annotation[] annotations,
>                              MediaType mediaType) {
>        return isJAXBObject(type, genericType) &&
> isSupportedMediaType(mediaType);
>    }
>
>    public Object readFrom(Class<Object> type,
>                           Type genericType,
>                           Annotation[] annotations,
>                           MediaType mediaType,
>                           MultivaluedMap<String, String> httpHeaders,
>                           InputStream entityStream) throws
> IOException, WebApplicationException {
>        Unmarshaller unmarshaller = null;
>        Object unmarshaledResource = null;
>        try {
>            JAXBContext context = getContext(type, mediaType);
>            unmarshaller = context.createUnmarshaller();
>            // set something here
>            unmarshaller.setProperty(..., ...);
>            if (type.isAnnotationPresent(XmlRootElement.class))
>                unmarshaledResource = unmarshaller.unmarshal(entityStream);
>            else
>                unmarshaledResource =
>                    unmarshaller.unmarshal(new
> StreamSource(entityStream), type).getValue();
>        } catch (JAXBException e) {
>            logger.error(Messages.getMessage("jaxbFailToUnmarshal"),
> type.getName());
>            throw new WebApplicationException(e,
> Response.Status.BAD_REQUEST);
>        }
>        return unmarshaledResource;
>    }
>
>    protected boolean isSupportedMediaType(MediaType mediaType) {
>        return MediaTypeUtils.isXmlType(mediaType);
>    }
>
>    public static boolean isJAXBObject(Class<?> type, Type genericType) {
>        return isXMLRootElement(type) || isXMLType(type);
>    }
> }
>
> Add MyXmlProvider to your JAX-RS Application subclass in the
> getClasses() method (or your wink-application config file).
>
> I would copy whatever you want out of the Wink source (i.e. don't
> extend from the internal code) to make sure your code works in future.
>
> 2009/11/2 അജിയോസ്‌ യോഹന്നാന്‍(Ajiyos) <aj...@gmail.com>:
> > I didnt get any response in user group. Trying my luck here.
> >
> > ---------- Forwarded message ----------
> > From: അജിയോസ്‌ യോഹന്നാന്‍(Ajiyos) <aj...@gmail.com>
> > Date: 2009/11/2
> > Subject: Question on JAXB ID resolver
> > To: wink-user@incubator.apache.org
> >
> >
> > Hi,
> >
> > Can any one tell me how to hookup my custom IDResolver to wink framework.
> >
> > The IDResolver need to be configured as a property to my jaxb
> unmarshaller.
> > However I coudnt find out how to get handle to unmarshaller.
> >
> > Thanks,
> > Ajiyos
> >
> > --
> > "Attitude not the Aptitude determines the Altitude"
> >
> >
> >
> > --
> > "Attitude not the Aptitude determines the Altitude"
> >
>



-- 
"Attitude not the Aptitude determines the Altitude"

Re: Question on JAXB ID resolver

Posted by Bryant Luk <br...@gmail.com>.
Hi Ajiyos,

You will need to write your own entity provider in this case (although
you can set properties on the marshalling side, the unmarshalling does
not currently).  If you want this feature, please open a JIRA against
Apache Wink (https://issues.apache.org/jira/browse/WINK) for future
consideration as time permits.

If you want a solution immediately, you can look at the source for our
JAXB XML providers here and base your solution on them:
http://svn.apache.org/repos/asf/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/

For instance, you could write something like:

@Provider
public MyXmlProvider implements MessageBodyReader<Object> {

    public boolean isReadable(Class<?> type,
                              Type genericType,
                              Annotation[] annotations,
                              MediaType mediaType) {
        return isJAXBObject(type, genericType) &&
isSupportedMediaType(mediaType);
    }

    public Object readFrom(Class<Object> type,
                           Type genericType,
                           Annotation[] annotations,
                           MediaType mediaType,
                           MultivaluedMap<String, String> httpHeaders,
                           InputStream entityStream) throws
IOException, WebApplicationException {
        Unmarshaller unmarshaller = null;
        Object unmarshaledResource = null;
        try {
            JAXBContext context = getContext(type, mediaType);
            unmarshaller = context.createUnmarshaller();
            // set something here
            unmarshaller.setProperty(..., ...);
            if (type.isAnnotationPresent(XmlRootElement.class))
                unmarshaledResource = unmarshaller.unmarshal(entityStream);
            else
                unmarshaledResource =
                    unmarshaller.unmarshal(new
StreamSource(entityStream), type).getValue();
        } catch (JAXBException e) {
            logger.error(Messages.getMessage("jaxbFailToUnmarshal"),
type.getName());
            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
        }
        return unmarshaledResource;
    }

    protected boolean isSupportedMediaType(MediaType mediaType) {
        return MediaTypeUtils.isXmlType(mediaType);
    }

    public static boolean isJAXBObject(Class<?> type, Type genericType) {
        return isXMLRootElement(type) || isXMLType(type);
    }
}

Add MyXmlProvider to your JAX-RS Application subclass in the
getClasses() method (or your wink-application config file).

I would copy whatever you want out of the Wink source (i.e. don't
extend from the internal code) to make sure your code works in future.

2009/11/2 അജിയോസ്‌ യോഹന്നാന്‍(Ajiyos) <aj...@gmail.com>:
> I didnt get any response in user group. Trying my luck here.
>
> ---------- Forwarded message ----------
> From: അജിയോസ്‌ യോഹന്നാന്‍(Ajiyos) <aj...@gmail.com>
> Date: 2009/11/2
> Subject: Question on JAXB ID resolver
> To: wink-user@incubator.apache.org
>
>
> Hi,
>
> Can any one tell me how to hookup my custom IDResolver to wink framework.
>
> The IDResolver need to be configured as a property to my jaxb unmarshaller.
> However I coudnt find out how to get handle to unmarshaller.
>
> Thanks,
> Ajiyos
>
> --
> "Attitude not the Aptitude determines the Altitude"
>
>
>
> --
> "Attitude not the Aptitude determines the Altitude"
>