Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Removed cross-package dependency on OpenSocialEnvironment from rave-p…
Browse files Browse the repository at this point in the history
…ortal (Supports RAVE-99)

git-svn-id: https://svn.apache.org/repos/asf/incubator/rave/trunk@1172942 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mfranklin committed Sep 20, 2011
1 parent e54f9da commit ed984cd
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 9 deletions.
Expand Up @@ -49,10 +49,6 @@ public class PageController {
private PageService pageService;
private UserService userService;

//TODO (RAVE-99) Figure out a better way to register script blocks so that we don't have to have this cross package dep
@Autowired
private OpenSocialEnvironment openSocialEnvironment;

@Autowired
public PageController(PageService pageService, UserService userService) {
this.pageService = pageService;
Expand All @@ -65,7 +61,6 @@ public String viewDefault(Model model) {
List<Page> pages = pageService.getAllPages(user.getEntityId());
model.addAttribute(ModelKeys.PAGE, pages.get(0));
model.addAttribute(ModelKeys.PAGES, pages);
model.addAttribute(ModelKeys.OPENSOCIAL_ENVIRONMENT, openSocialEnvironment);
return ViewNames.HOME;
}

Expand All @@ -79,7 +74,6 @@ public String view(@PathVariable Long pageId, Model model) {

model.addAttribute(ModelKeys.PAGE, page);
model.addAttribute(ModelKeys.PAGES, pages);
model.addAttribute(ModelKeys.OPENSOCIAL_ENVIRONMENT, openSocialEnvironment);
return ViewNames.HOME;
}
}
@@ -1,3 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.rave.portal.web.renderer;

/**
Expand Down
Expand Up @@ -20,15 +20,54 @@
package org.apache.rave.portal.web.tag;

import org.apache.rave.portal.web.renderer.RenderService;
import org.apache.rave.portal.web.renderer.ScriptLocation;
import org.apache.rave.web.tag.AbstractSingletonBeanDependentTag;
import serp.bytecode.RetInstruction;

import javax.servlet.jsp.JspException;
import java.util.List;

/**
* Renders a list of registered script blocks according to key
*/
public class ScriptTag extends AbstractSingletonBeanDependentTag<RenderService>{

private ScriptLocation location;

public ScriptTag() {
super(RenderService.class);
}

public ScriptLocation getLocation() {
return location;
}

public void setLocation(ScriptLocation location) {
this.location = location;
}

@Override
public int doStartTag() throws JspException {
int returnValue = SKIP_BODY;
RenderService renderService = getBean();
validateParams(renderService);
List<String> scripts = renderService.getScriptBlocks(location);
if(scripts != null) {
renderScripts(scripts);
returnValue = EVAL_BODY_INCLUDE;
}
return returnValue;
}

private void validateParams(RenderService renderService) throws JspException {
if(location == null || renderService == null) {
throw new JspException("Invalid configuration. Ensure that you have correctly configured the application and provided a location to the tag");
}
}

private void renderScripts(List<String> scripts) throws JspException {
for(String script : scripts) {
writeString(script);
}
}
}
Expand Up @@ -19,10 +19,24 @@

package org.apache.rave.provider.opensocial.config;

import org.apache.rave.portal.web.renderer.RenderService;
import org.apache.rave.portal.web.renderer.ScriptLocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
* Environment variables for OpenSocial calls from the portal (to Shindig)
*/
@Component
public class OpenSocialEnvironment {

private static final String SCRIPT_TEMPLATE = "<script src=\"%1$s://%2$s%3$s/js/container.js?c=1&amp;container=default&amp;debug=1\"></script>";

private RenderService renderService;

/**
* Protocol for the opensocial engine (http, https)
*/
Expand All @@ -36,7 +50,21 @@ public class OpenSocialEnvironment {
*/
private String engineGadgetPath;

@PostConstruct
public void init() {
renderService.registerScriptBlock(String.format(SCRIPT_TEMPLATE, engineProtocol, engineRoot, engineGadgetPath), ScriptLocation.BEFORE_RAVE);
}

public RenderService getRenderService() {
return renderService;
}

@Autowired
public void setRenderService(RenderService renderService) {
this.renderService = renderService;
}

@Value("${portal.opensocial_engine.protocol}")
public void setEngineProtocol(String engineProtocol) {
this.engineProtocol = engineProtocol;
}
Expand All @@ -45,6 +73,7 @@ public String getEngineProtocol() {
return engineProtocol;
}

@Value("${portal.opensocial_engine.root}")
public void setEngineRoot(String engineRoot) {
this.engineRoot = engineRoot;
}
Expand All @@ -53,6 +82,7 @@ public String getEngineRoot() {
return engineRoot;
}

@Value("${portal.opensocial_engine.gadget_path}")
public void setEngineGadgetPath(String engineGadgetPath) {
this.engineGadgetPath = engineGadgetPath;
}
Expand Down
13 changes: 13 additions & 0 deletions rave-portal/src/main/resources/META-INF/rave.tld
Expand Up @@ -37,4 +37,17 @@
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<description>Renders a set of script blocks for the current location</description>
<name>render-script</name>
<tag-class>org.apache.rave.portal.web.tag.ScriptTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description>The page location for the scripts</description>
<name>location</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>org.apache.rave.portal.web.renderer.ScriptLocation</type>
</attribute>
</tag>
</taglib>
8 changes: 5 additions & 3 deletions rave-portal/src/main/webapp/WEB-INF/views/home.jsp
@@ -1,3 +1,4 @@
<%@ page import="com.sun.xml.internal.ws.wsdl.writer.document.Import" %>
<%--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
Expand Down Expand Up @@ -26,8 +27,6 @@
<%@ taglib prefix="portal" uri="http://www.apache.org/rave/tags" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="rave"%>
<jsp:useBean id="pages" type="java.util.List<org.apache.rave.portal.model.Page>" scope="request"/>
<jsp:useBean id="openSocialEnv" scope="request" type="org.apache.rave.provider.opensocial.config.OpenSocialEnvironment"/>
<c:set var="opensocial_engine_url" value="${openSocialEnv.engineProtocol}://${openSocialEnv.engineRoot}${openSocialEnv.engineGadgetPath}"/>
<fmt:setBundle basename="messages"/>
<%--@elvariable id="page" type="org.apache.rave.portal.model.Page"--%>
<rave:rave_generic_page pageTitle="${page.name}">
Expand Down Expand Up @@ -157,16 +156,19 @@
</select>
</form>
</div>
<portal:render-script location="${'BEFORE_LIB'}" />
<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/jquery-ui.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js"></script>
<script src="${opensocial_engine_url}/js/container.js?c=1&amp;container=default&amp;debug=1"></script>
<portal:render-script location="${'AFTER_LIB'}" />
<portal:render-script location="${'BEFORE_RAVE'}" />
<script src="<spring:url value="/script/rave.js"/>"></script>
<script src="<spring:url value="/script/rave_api.js"/>"></script>
<script src="<spring:url value="/script/rave_opensocial.js"/>"></script>
<script src="<spring:url value="/script/rave_wookie.js"/>"></script>
<script src="<spring:url value="/script/rave_layout.js"/>"></script>
<portal:render-script location="${'AFTER_RAVE'}" />
<script>
//Define the global widgets variable
//This array will be populated by RegionWidgetRender providers.
Expand Down
@@ -0,0 +1,154 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.rave.portal.web.tag;

import org.apache.rave.portal.model.RegionWidget;
import org.apache.rave.portal.model.Widget;
import org.apache.rave.portal.web.renderer.RenderService;
import org.apache.rave.portal.web.renderer.ScriptLocation;
import org.junit.Before;
import org.junit.Test;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.easymock.EasyMock.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
*
*/
public class ScriptTagTest {
public static final String SCRIPT = "SCRIPT";
public static final String SCRIPT_2 = "SCRIPT2";
private ScriptTag tag;
private RenderService service;
private PageContext pageContext;

@Before
public void setup() throws JspException {
service = createNiceMock(RenderService.class);
WebApplicationContext context = createNiceMock(WebApplicationContext.class);
expect(context.getBean(RenderService.class)).andReturn(service).anyTimes();
replay(context);
ServletContext servletContext = createNiceMock(ServletContext.class);
expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).andReturn(context);
replay(servletContext);
pageContext = createNiceMock(PageContext.class);
expect(pageContext.getServletContext()).andReturn(servletContext).anyTimes();
tag = new ScriptTag();
tag.setPageContext(pageContext);
}

@Test
public void doStartTag_valid() throws IOException, JspException {

List<String> strings = new ArrayList<String>();
strings.add(SCRIPT);
expect(service.getScriptBlocks(ScriptLocation.BEFORE_RAVE)).andReturn(strings);
replay(service);

JspWriter writer = createNiceMock(JspWriter.class);
writer.print(SCRIPT);
expectLastCall();
replay(writer);

expect(pageContext.getOut()).andReturn(writer);
replay(pageContext);

tag.setLocation(ScriptLocation.BEFORE_RAVE);
int result = tag.doStartTag();
assertThat(result, is(equalTo(1)));
verify(writer);
}

@Test
public void doStartTag_multi() throws IOException, JspException {

List<String> strings = new ArrayList<String>();
strings.add(SCRIPT);
strings.add(SCRIPT_2);
expect(service.getScriptBlocks(ScriptLocation.BEFORE_RAVE)).andReturn(strings);
replay(service);

JspWriter writer = createNiceMock(JspWriter.class);
writer.print(SCRIPT);
expectLastCall();
writer.print(SCRIPT_2);
expectLastCall();
replay(writer);

expect(pageContext.getOut()).andReturn(writer).anyTimes();
replay(pageContext);

tag.setLocation(ScriptLocation.BEFORE_RAVE);
int result = tag.doStartTag();
assertThat(result, is(equalTo(1)));
verify(writer);
}

@Test
public void doStartTag_skip() throws IOException, JspException {

List<String> strings = new ArrayList<String>();
strings.add(SCRIPT);
strings.add(SCRIPT_2);
expect(service.getScriptBlocks(ScriptLocation.BEFORE_RAVE)).andReturn(null);
replay(service);

JspWriter writer = createNiceMock(JspWriter.class);
replay(writer);

expect(pageContext.getOut()).andReturn(writer).anyTimes();
replay(pageContext);

tag.setLocation(ScriptLocation.BEFORE_RAVE);
int result = tag.doStartTag();
assertThat(result, is(equalTo(TagSupport.SKIP_BODY)));
verify(writer);
}

@Test(expected = JspException.class)
public void doStartTag_null_location() throws IOException, JspException {

expect(service.getScriptBlocks(ScriptLocation.BEFORE_RAVE)).andReturn(null);
replay(service);

JspWriter writer = createNiceMock(JspWriter.class);
replay(writer);

expect(pageContext.getOut()).andReturn(writer);
replay(pageContext);

tag.setLocation(null);
int result = tag.doStartTag();
}

}

0 comments on commit ed984cd

Please sign in to comment.