{"id":69,"date":"2012-01-20T09:57:15","date_gmt":"2012-01-20T09:57:15","guid":{"rendered":"http:\/\/www.andreagirardi.it\/blog\/?p=69"},"modified":"2012-01-20T10:03:25","modified_gmt":"2012-01-20T10:03:25","slug":"query-alfresco-web-script-using-rest","status":"publish","type":"post","link":"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/","title":{"rendered":"Query Alfresco Web script using REST"},"content":{"rendered":"<p>With Spring 3 querying a REST-based service it&#8217;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:<\/p>\n<pre lang=\"html\">http:\/\/localhost:8081\/alfresco\/service\/api\/login?u=admin&amp;pw=admin<\/pre>\n<p>Assuming the userid and password are correct, Alfresco will respond back with this simple XML:<\/p>\n<pre lang=\"XML\"><!--?xml version=\"1.0\" encoding=\"UTF-8\"?-->\r\nTICKET_18d49dbd5de400c3fa1254b46e46ac51fbd0934e<\/pre>\n<p>Using Spring and Java you should write something like that:<\/p>\n<pre lang=\"java\">RestTemplate restTemplate = new RestTemplate();\r\nMap params = new HashMap();\r\nparams.put(\"user\", login);\r\nparams.put(\"password\", password);\r\nString url = \"http:\/\/localhost:8081\/alfresco\/service\/api\/login?u={user}&amp;pw={password}\";\r\nSource result = restTemplate.getForObject(url, Source.class, params);\r\nXPathOperations xpath = new Jaxp13XPathTemplate();\t\t\t\r\nthis.authenticationTicket = xpath.evaluateAsString(\"\/\/ticket\", result);<\/pre>\n<p>The <a title=\"REST in Spring3: RestTemplate\" href=\"http:\/\/blog.springsource.org\/2009\/03\/27\/rest-in-spring-3-resttemplate\/\">RestTemplate<\/a> 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.<\/p>\n<p>Once authenticated, we are ready to call Web script to query Alfresco repository:<\/p>\n<pre lang=\"html\">http:\/\/localhost:8081\/alfresco\/service\/kipcast\/search\/brand.xml?q={keyword}&alf_ticket={ticket}<\/pre>\n<p>This web script returns an XML like that:<\/p>\n<pre lang=\"XML\"><brands>\r\n\t<brand>\r\n\t\t<nodeRef>a8a4c38d-67fb-475b-84cb-9cf912dfac58<\/nodeRef>\r\n\t\t<name>Name1<\/name>\r\n\t\t<supplierName>\r\n\t\t\tCompany1\r\n\t\t<\/supplierName>\t\t\t\t\t\r\n\t\t<relatedCommonName>\r\n\t\t\tRelatedCommonName1\r\n\t\t<\/relatedCommonName>\r\n\t<\/brand>\r\n<\/brands><\/pre>\n<p>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:<\/p>\n<pre lang=\"XML\"><bean id=\"restTemplate\" class=\"org.springframework.web.client.RestTemplate\">\r\n    <property name=\"messageConverters\">\r\n        <list>\r\n            <bean id=\"messageConverter\" class=\"org.springframework.http.converter.xml.MarshallingHttpMessageConverter\">\r\n                <property name=\"marshaller\" ref=\"marshaller\" \/>\r\n                <property name=\"unmarshaller\" ref=\"marshaller\" \/>\r\n            <\/bean>\r\n        <\/list>\r\n    <\/property>        \r\n<\/bean>\r\n\r\n\r\n<bean name=\"marshaller\" class=\"org.springframework.oxm.xstream.XStreamMarshaller\">\r\n    <property name=\"autodetectAnnotations\" value=\"true\"\/>\r\n    <property name=\"annotatedClasses\">\r\n        <array>\r\n            <value>com.kipcast.dataModel.drugs.bean.Brands<\/value>\r\n            <value>com.kipcast.dataModel.drugs.bean.Brand<\/value>\r\n        <\/array>\r\n    <\/property>\r\n<\/bean><\/pre>\n<p>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:<\/p>\n<pre lang=\"java\">ApplicationContext applicationContext = new ClassPathXmlApplicationContext(\"alfresco-context.xml\");\r\nRestTemplate restTemplate = applicationContext.getBean(\"restTemplate\", RestTemplate.class);\t\r\n\r\nMap<String, String> params = new HashMap<String, String>();\r\nparams.put(\"ticket\", this.authenticationTicket);\t\r\nparams.put(\"keyword\", key);\t\r\nString url = \"http:\/\/localhost:8081\/alfresco\/service\/kipcast\/search\/brand.xml?q={keyword}&alf_ticket={ticket}\";\r\nBrands brandList = (Brands) restTemplate.getForObject(url, Brands.class, params);<\/pre>\n<p>Last thing is modify the two beans Brands and Brand putting the proper annotations to both classes:<\/p>\n<pre lang=\"java\">\r\n@XStreamAlias(\"brands\")\r\npublic class Brands {\r\n\r\n\t@XStreamImplicit\r\n\tList<Brand> brandList = new ArrayList<Brand>(); \r\n\t  \r\n    public List<Brand> getBrands() {\r\n        return brandList;\r\n    }\r\n\t\t  \r\n\tpublic void setBrands(List<Brand> brandList) {\r\n\t    this.brandList = brandList;\r\n\t}\r\n\t\r\n\tpublic void addBrands(Brand brand) {\r\n\t    this.brandList.add(brand);\r\n\t}\r\n\t\r\n}\r\n\r\n@XStreamAlias(\"brand\")\r\npublic class Brand {\r\n\r\n\tprivate String nodeRef;\r\n\tprivate String name;\r\n\tprivate String relatedCommonName;\r\n\tprivate String supplierName;\r\n    \r\n        \/\/ Getter and setter methods\r\n}\r\n\r\n<\/pre>\n<p>For more details please take a look to <a href=\"http:\/\/thekspace.com\/home\/component\/content\/article\/57-restful-clients-in-spring-3.html\">thekspace.com<\/a>, <a href=\"http:\/\/tedwise.com\/2010\/06\/14\/accessing-rest-services-from-spring-3\/\">tedwise.com<\/a> and <a href=\"http:\/\/blog.springsource.com\/2009\/03\/27\/rest-in-spring-3-resttemplate\/\">springsource.com<\/a>. <\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-69\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-69\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-69\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-69\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>With Spring 3 querying a REST-based service it&#8217;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&amp;pw=admin Assuming the userid and password [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-69\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-69\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-69\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-69\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/query-alfresco-web-script-using-rest\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[14],"tags":[12,4,13],"_links":{"self":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/69"}],"collection":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":16,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/69\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}