Quản lý cấu hình web - part 37 ppsx

10 183 0
Quản lý cấu hình web - part 37 ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Integrating WCM Using Web Scripts [ 342 ] Calling web scripts from a JSP page Now we will create a simple JSP page from which we will make a call to our web script to render the news page. This JSP can be a part of any of your web applications. To call a web script from the JSP, this will be an HTTP call. Just add the following scriptlet in the JSP le wherever you want to include the HTML output of this web script: <% String resultString = ""; HttpClient client = new HttpClient(); client.getState().setCredentials( new AuthScope("localhost", 8080, "Alfresco"), new UsernamePasswordCredentials("admin", "admin") ); GetMethod get = new GetMethod("http://localhost:8080/alfresco/service/org/cignex/news/ getNewsItem.html?storeId=wwwcignex&newsId=newsAlfrescoBookRelease. xml"); get.setDoAuthentication(true); try { int status = client.executeMethod( get ); resultString = get.getResponseBodyAsString(); out.println(resultString); } finally { get.releaseConnection(); } %> Because we are using an HTTP client, we also need to import the used classes here in this scriptlet before using it. To import those, we can use the page directive of JSP as follows: <%@ page import="org.apache.commons.httpclient.HttpClient" %> <%@ page import="org.apache.commons.httpclient. UsernamePasswordCredentials" %> <%@ page import="org.apache.commons.httpclient.auth.AuthScope" %> <%@ page import="org.apache.commons.httpclient.methods.GetMethod" %> This will import all of the required classes. Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 343 ] The source les for the whole JSP page can be downloaded from the Packt website. The following screenshot is the output of that JSP page. This JSP page is part of a sample web application, which is outside of Alfresco and deployed on a separate Tomcat web server: Download from Wow! eBook <www.wowebook.com> Integrating WCM Using Web Scripts [ 344 ] Enhancing the news item web script In this case study, we will make enhancements to the previously mentioned news web script. Consider a case where you have some part of the content that is being used in multiple places, such as some contact number, e-mail address, link URLs, and so on. When this kind of content needs to be modied in the future, you will need to modify it at multiple places. Instead, we can have a concept where we store this kind of content at one place and then refer that in all of the places when we want to actually use it. In this way, it will enable reusability and it will be easier to modify it in the future. No longer will we have to modify the same content at multiple places; we just need to modify it at one place and that will be reected in all of the places where it is being used. Consider this kind of content as tag and store it separately in one place only. We can then refer to it with its tag ID in the actual content when it needs to be used. This means we will have one tag mapping XML le where we will store all these kinds of tags with some ID and value. And then while creating any content, if we want to use it, we will refer it with the ID, and at the time of rendering the response through the web script, it will replace those tag IDs with actual values. Create a TagMapping.xml le in the /ROOT/common folder under your web project using the tag_mapping web form. Download the XSD le for this web form from the Packt website. In the news content used in the previous case study, the website URL for the book is used at multiple places. We will create a tag for this and then use this tag in the news content. A sample TagMapping.xml le looks like: <?xml version="1.0" encoding="UTF-8"?> <items xmlns:alf="http://www.alfresco.org" xmlns:chiba="http:// chiba.sourceforge.net/xforms" xmlns:ev="http://www.w3.org/2001/xml- events" xmlns:xf="http://www.w3.org/2002/xforms" xmlns:xhtml="http:// www.w3.org/1999/xhtml" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <item> <id>phone_number</id> <value>1-408-923-4231</value> </item> <item> <id>book_website</id> Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 345 ] <value>http://www.packtpub.com/alfresco-3-enterprise-content- management-implementation/book/mid/160609knbhtv</value> </item> </items> Here we have created a tag for the URL of the website for our Alfresco book with ID book_website, and in value we have specied the URL for this. Now when we create XML content for the news, wherever we want to mention the book URL, instead of writing the actual URL, we will refer this tag with ID book_website as: <a href="${book_website}">Alfresco 3 Enterprise Content Management Implementation</a> We need to use it as ${tag_id} only (syntax for a FreeMarker template), because we are going to use the template service to replace this tag's runtime with the actual value of this tag. Web script for getting the details of a particular news item Now let's look at the web script changes for this. In this example, we will see how we can fetch the details of an individual news item. Description document The description document, getNewsItem.get.desc.xml, will be the same as used in the previous example. Java-backed Bean for web scripts We use the same Java Bean class, GetNewsItem, which extends the DeclarativeWebScript class, but we will modify this class as follows: 1. Render the intermediate template model ( getNewsItem.get.html.model. ftl ) with the renderTemplate method of template service as: renderTemplate(templatePath, map, out); where templatePath is the path for the le getNewsItem.get.html.model. ftl , map is the model map of the news item, and out is used to store the intermediate result of this rendering. Download from Wow! eBook <www.wowebook.com> Integrating WCM Using Web Scripts [ 346 ] 2. Create a tag model map by processing the TagMapping.xml le. 3. Then process this tag model over the result of that intermediate template rendering with the processTagging method of template service as: result = templateService.processTemplateString( "freemarker", template, modelMap); where freemarker indicates the use of FreeMarker as the template processor, template is the intermediate result generated by rendering of the template in the previous step, modelMap is the model map created by processing the tag mapping le, and result will hold the nal result after replacing all of the tags with actual values. Here we are using the template service to render and process the FreeMarker template. 4. Now in the Spring Bean conguration le, web-script-custom-context.xml, in the tomcat/webapps/alfresco/WEB-INF/classes/alfresco/extension folder, we need to add template service as a property for that Bean as we are using this new service for our example as: <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'> <beans> <! Web Script Storage > <bean id="webscript.org.cignex.news.getNewsItem.get" class="com. cignex.web.scripts.bean.news.GetNewsItem" parent="webscript"> <property name="contentService" ref="ContentService" /> <property name="avmService" ref="AVMService" /> <property name="templateService" ref="TemplateService" /> </bean> </beans> Response template The intermediate rendering template for the HTML response, the getNewsItem. get.html.model.ftl le is as follows: <#ftl ns_prefixes={"D", "http://www.alfrescobook.com/news"}> <#if m_news?exists> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr> <#assign newsItem = m_news> <td> <h2>${newsItem.contentHeader}</h2> <#if newsItem.newsDate != ""> Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 347 ] <strong>News Date:</strong> ${newsItem.newsDate} </#if> <#if newsItem.contentSubHeader != "" > <h4>${newsItem.contentSubHeader}</h4> </#if> <table width="50%" cellspacing="0" cellpadding="5" border="0"> <tr> <td> <tr> <h4> ${newsItem.imageTitle}</h4> <#if newsItem.contentGraphic != "" > <td> <img src="${newsItem.contentGraphic}" border=0/> </td> </#if> </tr> </td> <td> <strong> ${newsItem.imageCaption}</strong> </td> </tr> <tr> <td> ${newsItem.contentText} </td> </tr> </table> </td> </tr> </table> </#if> </html> And nally, the rendering template, the getNewsItem.get.html.ftl le, is: <html> ${finalXML} </html> Download from Wow! eBook <www.wowebook.com> Integrating WCM Using Web Scripts [ 348 ] Storing / registering the web script in Alfresco There is no change in the description document, but upload the new template le getNewsItem.get.html.model.ftl in the Company Home | Data Dictionary | Web Scripts Extensions | org | cignex | news folder. Also, modify the getNewsItem.get.html.ftl le that already exists there accordingly. Navigate to http://localhost:8080/alfresco/service/index and click on the Refresh Web Scripts button to view the changes. Calling the web script from a JSP page There will not be any changes for this. The same JSP that was used in the previous case can be used to call this web script, and the same result will be displayed: As shown in the previous screenshot, the actual book page URL being displayed in the status bar comes from the tag mapping le. Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 349 ] You can download the code samples for web script-related and blog module-related les for this case study from the Packt website. Integrating Alfresco WCM and a Surf-based web application In this case study, we will refer to the same web script we used in the rst case study (Integrating Alfresco WCM and Liferay with a news portlet). For integrating this with a Surf-based web application, we need to create a JSON response format for the same web script. Refer to the Integrating Alfresco WCM and Liferay with a news portlet section for more details on the web script for getting a news headline. Response template The rendering template for the JSON response, the getNewsItem.get.json.ftl le, is as follows: <#escape x as jsonUtils.encodeJSONString(x)> { "newsItems": [ <#if m_newsNodes?exists> <#list m_newsNodes as newsNode> <#assign newsItemDom = newsNode.xmlNodeModel/> <#if newsItemDom?exists> <#assign newsItem = newsItemDom.news> <#if newsItem?exists> { "Headline":"${newsItem.contentHeader}" } </#if> </#if> <#if newsNode_has_next>,</#if> </#list> </#if> ] } </#escape> Download from Wow! eBook <www.wowebook.com> Integrating WCM Using Web Scripts [ 350 ] Integrating web scripts with a SURF application In the Communication with Web Content Management section of Chapter 9, we have already explained the conguration required at Surf side to call web scripts from Alfresco, along with a UI web script that will be responsible for rendering the page. And the web script we mentioned earlier in this section is a data web script, which will be responsible for fetching content from the repository. In Surf, we have already created a header web script to display the main page. We have also created a web script to display the news header in Surf. Now we will call the news web script in the same (header) web script to integrate news headline on the same page. The following screenshot shows the main page from the Surf application where we have integrated the WCM web script. Content is fetched from the repository: You can download the code samples from the Packt website. Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 351 ] Summary In this chapter, we covered the REST architecture at high level and saw how an Alfresco web script allows users to integrate Alfresco with any external application easily, independent of the technology. In this way, you can use Alfresco WCM purely for content authoring and as a content production system without worrying about the frontend rendering of it. Hence, the same Alfresco WCM content can be used and rendered in different ways with multiple applications. Again, web scripts support different output formats, including XML, JSON, and so on, so you can generate the output in the format expected by the frontend application very easily. Mainly we discussed: • REST architecture and web script framework in Alfresco • Different components of a web script • How to develop web scripts in Alfresco • How to integrate Alfresco WCM with external systems using web scripts In the next chapter, we will discuss how we can leverage the Alfresco framework for WCM. Download from Wow! eBook <www.wowebook.com> . FreeMarker template. 4. Now in the Spring Bean conguration le, web- script-custom-context.xml, in the tomcat/webapps/alfresco /WEB- INF/classes/alfresco/extension folder, we need to add template. <id>book_website</id> Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 345 ] <value>http://www.packtpub.com/alfresco-3-enterprise-content- management-implementation/book/mid/160609knbhtv</value> . xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <item> <id>phone_number</id> <value> 1-4 0 8-9 2 3-4 231</value> </item> <item> <id>book_website</id> Download

Ngày đăng: 05/07/2014, 20:21

Tài liệu cùng người dùng

Tài liệu liên quan