With Spring 3 querying a REST-based service it’s easy. For first, you need to authenticate on Alfresco repository using an Alfresco Ticket instead of an explicit user name and password. A ticket represents a pre-authenticated user who has already performed the login process. To log in, the url is:
http://localhost:8081/alfresco/service/api/login?u=admin&pw=admin |
http://localhost:8081/alfresco/service/api/login?u=admin&pw=admin
Assuming the userid and password are correct, Alfresco will respond back with this simple XML:
<!--?xml version="1.0" encoding="UTF-8"?-->
TICKET_18d49dbd5de400c3fa1254b46e46ac51fbd0934e |
<!--?xml version="1.0" encoding="UTF-8"?-->
TICKET_18d49dbd5de400c3fa1254b46e46ac51fbd0934e
Using Spring and Java you should write something like that:
RestTemplate restTemplate = new RestTemplate();
Map params = new HashMap();
params.put("user", login);
params.put("password", password);
String url = "http://localhost:8081/alfresco/service/api/login?u={user}&pw={password}";
Source result = restTemplate.getForObject(url, Source.class, params);
XPathOperations xpath = new Jaxp13XPathTemplate();
this.authenticationTicket = xpath.evaluateAsString("//ticket", result); |
RestTemplate restTemplate = new RestTemplate();
Map params = new HashMap();
params.put("user", login);
params.put("password", password);
String url = "http://localhost:8081/alfresco/service/api/login?u={user}&pw={password}";
Source result = restTemplate.getForObject(url, Source.class, params);
XPathOperations xpath = new Jaxp13XPathTemplate();
this.authenticationTicket = xpath.evaluateAsString("//ticket", result);
The RestTemplate was introduced in Spring 3 to give REST access the same support as JDBC, passing a Map of keywords and values. It serves as a wrapper to hide all the details in accessing REST resources, including opening and closing connection objects and converting responses to Java objects. Source class is the return type.
Once authenticated, we are ready to call Web script to query Alfresco repository:
http://localhost:8081/alfresco/service/kipcast/search/brand.xml?q={keyword}&alf_ticket={ticket} |
http://localhost:8081/alfresco/service/kipcast/search/brand.xml?q={keyword}&alf_ticket={ticket}
This web script returns an XML like that:
<brands>
<brand>
<nodeRef>a8a4c38d-67fb-475b-84cb-9cf912dfac58</nodeRef>
<name>Name1</name>
<supplierName>
Company1
</supplierName>
<relatedCommonName>
RelatedCommonName1
</relatedCommonName>
</brand>
</brands> |
<brands>
<brand>
<nodeRef>a8a4c38d-67fb-475b-84cb-9cf912dfac58</nodeRef>
<name>Name1</name>
<supplierName>
Company1
</supplierName>
<relatedCommonName>
RelatedCommonName1
</relatedCommonName>
</brand>
</brands>
The above response needs some configuration to marshall objects to XML. All we need to do is define it in our application context and, for that to work fine, you need to tell the marshaller which annotated classes you have:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller" />
</bean>
</list>
</property>
</bean>
<bean name="marshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true"/>
<property name="annotatedClasses">
<array>
<value>com.kipcast.dataModel.drugs.bean.Brands</value>
<value>com.kipcast.dataModel.drugs.bean.Brand</value>
</array>
</property>
</bean> |
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller" />
</bean>
</list>
</property>
</bean>
<bean name="marshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true"/>
<property name="annotatedClasses">
<array>
<value>com.kipcast.dataModel.drugs.bean.Brands</value>
<value>com.kipcast.dataModel.drugs.bean.Brand</value>
</array>
</property>
</bean>
At this point we can simply instantiate a RestTemplate object by calling the getBean method on the application context. This is the Java code to do that:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("alfresco-context.xml");
RestTemplate restTemplate = applicationContext.getBean("restTemplate", RestTemplate.class);
Map<String, String> params = new HashMap<String, String>();
params.put("ticket", this.authenticationTicket);
params.put("keyword", key);
String url = "http://localhost:8081/alfresco/service/kipcast/search/brand.xml?q={keyword}&alf_ticket={ticket}";
Brands brandList = (Brands) restTemplate.getForObject(url, Brands.class, params); |
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("alfresco-context.xml");
RestTemplate restTemplate = applicationContext.getBean("restTemplate", RestTemplate.class);
Map<String, String> params = new HashMap<String, String>();
params.put("ticket", this.authenticationTicket);
params.put("keyword", key);
String url = "http://localhost:8081/alfresco/service/kipcast/search/brand.xml?q={keyword}&alf_ticket={ticket}";
Brands brandList = (Brands) restTemplate.getForObject(url, Brands.class, params);
Last thing is modify the two beans Brands and Brand putting the proper annotations to both classes:
@XStreamAlias("brands")
public class Brands {
@XStreamImplicit
List<Brand> brandList = new ArrayList<Brand>();
public List<Brand> getBrands() {
return brandList;
}
public void setBrands(List<Brand> brandList) {
this.brandList = brandList;
}
public void addBrands(Brand brand) {
this.brandList.add(brand);
}
}
@XStreamAlias("brand")
public class Brand {
private String nodeRef;
private String name;
private String relatedCommonName;
private String supplierName;
// Getter and setter methods
} |
@XStreamAlias("brands")
public class Brands {
@XStreamImplicit
List<Brand> brandList = new ArrayList<Brand>();
public List<Brand> getBrands() {
return brandList;
}
public void setBrands(List<Brand> brandList) {
this.brandList = brandList;
}
public void addBrands(Brand brand) {
this.brandList.add(brand);
}
}
@XStreamAlias("brand")
public class Brand {
private String nodeRef;
private String name;
private String relatedCommonName;
private String supplierName;
// Getter and setter methods
}
For more details please take a look to thekspace.com, tedwise.com and springsource.com.