incubator-wink-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bryant Luk <bryant....@gmail.com>
Subject Re: Question on JAXB ID resolver
Date Mon, 02 Nov 2009 20:23:30 GMT
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) <ajiyos@gmail.com>:
> I didnt get any response in user group. Trying my luck here.
>
> ---------- Forwarded message ----------
> From: അജിയോസ്‌ യോഹന്നാന്‍(Ajiyos) <ajiyos@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"
>

Mime
View raw message