You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@wink.apache.org by "Zhao, Kun (Caron,HPSW-BTO-R&D-SH)" <ku...@hp.com> on 2009/11/18 02:16:57 UTC

Question of Media type Mapping in Wink 1.0

Hi,

We are doing porting our REST service from Wink0.1  to Wink1.0.

we met the issue that the default "Media type Mapping" setting didn't work any longer in Wink 1.0.

In Wink 0.1 it supports access a certain atom entry by Browser like IE, and it changes the "Content-Type" to "text/xml" in response automatically.
And this makes browser can display atom entry xml file correctly, by default setting in Wink 0.1.
But in Wink 1.0 it doesn't work, so I followed "Apache wink 1.0 user guide", the chapter 5.5 "Spring Integration"  Customizing Media-Type Mappings said:
<bean id="custom.MediaTypeMapper" class="org.apache.wink.server.internal.MediaTypeMapper">
<property name="mappings">
<list>
<map>
<entry key="userAgentStartsWith" value="Mozilla/" />
<entry key="resultMediaType">
<util:constant static-field=" javax.ws.rs.core.MediaType.ATOM" />
</entry>
<entry key="typeToSend">
<util:constant static-field="javax.ws.rs.core.MediaType.TEXT_XML" />
</entry>
</map>
</list>
</property>
</bean>

Actually, this is wrong, because These is no field named javax.ws.rs.core.MediaType.ATOM  in MediaType and the org.apache.wink.server.internal.MediaTypeMapper doesn't have a public property named "mappings", so it doesn't work.

Then I followed wink Api doc, I created a subclass to extend "org.apache.wink.server.handlers.MediaTypeMapperFactory",
But it still didn't work either I set it in Wink config file like this :
                wink.mediaTypeMapperFactoryClass=com.hp.sm.dao.wink.CustomizedMediaTypeMapper
or set it by Spring like this:
                <bean id="custom.MediaTypeMapper" class="com.hp.sm.dao.wink.CustomizedMediaTypeMapper">
                <bean id="customConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                                <property name="ignoreUnresolvablePlaceholders" value="true" />
                                <property name="order" value="1" />
                                <property name="propertiesArray">
                                                <list>
                                                                <props>
                                                                                <prop key="wink.MediaTypeMapper">custom.MediaTypeMapper</prop>
                                                                                <prop key="wink.propertiesFactory">customPropertiesFactory</prop>
                                                                </props>
                                                </list>
                                </property>
                </bean>

Do you know how to enable this "Media type Mapping" correctly? Attached my CustomizedMediaTypeMapper content. Log shows the method getMediaTypeMappings() never be called.


11:public class CustomizedMediaTypeMapper extends MediaTypeMapperFactory{
12:    private static Logger log = Logger.getLogger(CustomizedMediaTypeMapper.class.getName());
13:    private List<MappingRecord> records;
14:    public CustomizedMediaTypeMapper(){
15:        log.info("CustomizedMediaTypeMapper is created");
16:        records = new ArrayList();
17:        records.add(new MappingRecord());
18:    }
19:
20:    public List<? extends MediaTypeMappingRecord> getMediaTypeMappings(){
21:        log.info("in getMediaTypeMappings()");
22:        return records;
23:    }
24:
25:    private static class MappingRecord implements MediaTypeMappingRecord{
26:        public MediaType match(HttpHeaders requestHeaders,MediaType responseMediaType){
27:            List<String> header = requestHeaders.getRequestHeader(requestHeaders.USER_AGENT);
28:            if(log.isLoggable(Level.INFO))
29:                log.info("response media type = "+responseMediaType.getSubtype());
30:            boolean isAtom = responseMediaType.getSubtype().contains("atom");
31:            if(!isAtom)
32:                return responseMediaType;
33:            for(String h:header){
34:                if(log.isLoggable(Level.FINE))
35:                    log.fine("header:"+h);
36:                if(h.contains("Mozilla") && isAtom){
37:                    return MediaType.TEXT_XML_TYPE;
38:                }
39:            }
40:            return responseMediaType;
41:        }
42:    }
43:}

Regards,
Caron


Re: Question of Media type Mapping in Wink 1.0

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

I did some investigation here and while the Spring configuration
documentation may be outdated, I expected setting:
wink.mediaTypeMapperFactoryClass=com.hp.sm.dao.wink.CustomizedMediaTypeMapper
in your Wink configuration file to work.  I'm a bit puzzled why it
doesn't.  Unfortunately, I don't have that much Spring experience.

http://svn.apache.org/repos/asf/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/handlers/MediaTypeMappingTest.java
is an example of a unit test.

http://svn.apache.org/repos/asf/incubator/wink/trunk/wink-server/src/test/resources/org/apache/wink/server/internal/handlers/MediaTypeMappingTest.properties
is the configuration.

If you turn on debug trace for org.apache.wink.* then you should also
see a message like "MediaTypeMappingFactory Class is: <class name>" to
see if Wink is picking up the property correctly.  This debug trace is
in org.apache.wink.server.internal.DeploymentConfiguration#initMapper().

On Tue, Nov 17, 2009 at 7:16 PM, Zhao, Kun (Caron,HPSW-BTO-R&D-SH)
<ku...@hp.com> wrote:
> Hi,
>
>
>
> We are doing porting our REST service from Wink0.1  to Wink1.0.
>
>
>
> we met the issue that the default “Media type Mapping” setting didn’t work
> any longer in Wink 1.0.
>
>
>
> In Wink 0.1 it supports access a certain atom entry by Browser like IE, and
> it changes the “Content-Type” to “text/xml” in response automatically.
>
> And this makes browser can display atom entry xml file correctly, by default
> setting in Wink 0.1.
>
> But in Wink 1.0 it doesn’t work, so I followed “Apache wink 1.0 user guide”,
> the chapter 5.5 “Spring Integration”  Customizing Media-Type Mappings said:
>
> <bean id="custom.MediaTypeMapper"
> class="org.apache.wink.server.internal.MediaTypeMapper">
>
> <property name="mappings">
>
> <list>
>
> <map>
>
> <entry key="userAgentStartsWith" value="Mozilla/" />
>
> <entry key="resultMediaType">
>
> <util:constant static-field=" javax.ws.rs.core.MediaType.ATOM" />
>
> </entry>
>
> <entry key="typeToSend">
>
> <util:constant static-field="javax.ws.rs.core.MediaType.TEXT_XML" />
>
> </entry>
>
> </map>
>
> </list>
>
> </property>
>
> </bean>
>
>
>
> Actually, this is wrong, because These is no field named
> javax.ws.rs.core.MediaType.ATOM  in MediaType and the
> org.apache.wink.server.internal.MediaTypeMapper doesn’t have a public
> property named “mappings”, so it doesn’t work.
>
>
>
> Then I followed wink Api doc, I created a subclass to extend
> “org.apache.wink.server.handlers.MediaTypeMapperFactory”,
>
> But it still didn’t work either I set it in Wink config file like this :
>
>
> wink.mediaTypeMapperFactoryClass=com.hp.sm.dao.wink.CustomizedMediaTypeMapper
>
> or set it by Spring like this:
>
>                 <bean id="custom.MediaTypeMapper"
> class="com.hp.sm.dao.wink.CustomizedMediaTypeMapper">
>
>                 <bean id="customConfigurer"
> class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
>
>                                 <property
> name="ignoreUnresolvablePlaceholders" value="true" />
>
>                                 <property name="order" value="1" />
>
>                                 <property name="propertiesArray">
>
>                                                 <list>
>
>                                                                 <props>
>
>
> <prop key="wink.MediaTypeMapper">custom.MediaTypeMapper</prop>
>
>
> <prop key="wink.propertiesFactory">customPropertiesFactory</prop>
>
>                                                                 </props>
>
>                                                 </list>
>
>                                 </property>
>
>                 </bean>
>
>
>
> Do you know how to enable this “Media type Mapping” correctly? Attached my
> CustomizedMediaTypeMapper content. Log shows the method
> getMediaTypeMappings() never be called.
>
>
>
>
>
> 11:public class CustomizedMediaTypeMapper extends MediaTypeMapperFactory{
>
> 12:    private static Logger log =
> Logger.getLogger(CustomizedMediaTypeMapper.class.getName());
>
> 13:    private List<MappingRecord> records;
>
> 14:    public CustomizedMediaTypeMapper(){
>
> 15:        log.info("CustomizedMediaTypeMapper is created");
>
> 16:        records = new ArrayList();
>
> 17:        records.add(new MappingRecord());
>
> 18:    }
>
> 19:
>
> 20:    public List<? extends MediaTypeMappingRecord> getMediaTypeMappings(){
>
> 21:        log.info("in getMediaTypeMappings()");
>
> 22:        return records;
>
> 23:    }
>
> 24:
>
> 25:    private static class MappingRecord implements MediaTypeMappingRecord{
>
> 26:        public MediaType match(HttpHeaders requestHeaders,MediaType
> responseMediaType){
>
> 27:            List<String> header =
> requestHeaders.getRequestHeader(requestHeaders.USER_AGENT);
>
> 28:            if(log.isLoggable(Level.INFO))
>
> 29:                log.info("response media type =
> "+responseMediaType.getSubtype());
>
> 30:            boolean isAtom =
> responseMediaType.getSubtype().contains("atom");
>
> 31:            if(!isAtom)
>
> 32:                return responseMediaType;
>
> 33:            for(String h:header){
>
> 34:                if(log.isLoggable(Level.FINE))
>
> 35:                    log.fine("header:"+h);
>
> 36:                if(h.contains("Mozilla") && isAtom){
>
> 37:                    return MediaType.TEXT_XML_TYPE;
>
> 38:                }
>
> 39:            }
>
> 40:            return responseMediaType;
>
> 41:        }
>
> 42:    }
>
> 43:}
>
>
>
> Regards,
>
> Caron
>
>