Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
[TestCase] JuzuPortlet create view,action url in resource phase
Browse files Browse the repository at this point in the history
  • Loading branch information
haint authored and vietj committed Jul 31, 2013
1 parent 7412fb0 commit 2e8ab11
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 123 deletions.
Expand Up @@ -22,6 +22,8 @@
import java.io.Writer;
import java.util.Map;

import juzu.impl.request.ContextLifeCycle;
import juzu.impl.request.Request;
import juzu.request.Phase;
import org.gatein.common.net.media.MediaType;
import org.gatein.pc.api.ActionURL;
Expand Down Expand Up @@ -52,10 +54,14 @@ class GateInPortletInvocationContext implements PortletInvocationContext {
/** . */
private final PortletContent state;

GateInPortletInvocationContext(PortletContentProvider portletManager, WindowContext window) {
/** . */
private final ContextLifeCycle lifeCycle;

GateInPortletInvocationContext(PortletContentProvider portletManager, WindowContext window, ContextLifeCycle lifeCycle) {
this.portletManager = portletManager;
this.window = window;
this.state = (PortletContent) window.state;
this.lifeCycle = lifeCycle;
}

@Override
Expand All @@ -70,6 +76,29 @@ public String encodeResourceURL(String url) throws IllegalArgumentException {

@Override
public String renderURL(ContainerURL containerURL, URLFormat format) {
Request current = Request.getCurrent();
if (current != null) {
ContextLifeCycle currentLF = current.suspend();
try {
return doRenderURL(containerURL, format);
} finally {
currentLF.resume();
}
} else {
return doRenderURL(containerURL, format);
}
}

private String doRenderURL(ContainerURL containerURL, URLFormat format) {
lifeCycle.resume();
try {
return getDispatch(containerURL).toString();
} finally {
Request.getCurrent().suspend();
}
}

private Phase.View.Dispatch getDispatch(ContainerURL containerURL) {
Phase.View.Dispatch dispatch;
if (containerURL instanceof RenderURL) {
RenderURL renderURL = (RenderURL) containerURL;
Expand Down Expand Up @@ -163,7 +192,7 @@ public String renderURL(ContainerURL containerURL, URLFormat format) {
dispatch.setParameter("javax.portlet.r", id);
}
}
return dispatch.toString();
return dispatch;
}

@Override
Expand Down
Expand Up @@ -110,34 +110,35 @@ public Response processAction(
Mode mode_ = mode != null ? Mode.create(mode) : Mode.VIEW;

//
ActionInvocation action = new ActionInvocation(new GateInPortletInvocationContext(this, window));
PortletContent state = (PortletContent) window.state;
action.setClientContext(new GateInClientContext());
action.setPortalContext(new AbstractPortalContext());
action.setInstanceContext(new AbstractInstanceContext(window.name, AccessMode.READ_ONLY));
action.setWindowContext(new AbstractWindowContext(window.name));
action.setUserContext(new AbstractUserContext());
action.setSecurityContext(new AbstractSecurityContext(Context.getCurrentRequest()));
action.setRequest(Context.getCurrentRequest());
action.setResponse(Context.getCurrentResponse());
action.setTarget(state.portlet.getContext());
action.setMode(mode_);
action.setWindowState(windowState_);
action.setNavigationalState(ParametersStateString.create());
action.setInteractionState(interactionState != null ? ParametersStateString.create(interactionState) : null);
action.setPublicNavigationalState(window.computePublicParameters());

//
PortletInvoker invoker = portletManager.getInvoker();
Request current = Request.getCurrent();
ContextLifeCycle clf = current.suspend();
ContextLifeCycle lifeCycle = Request.getCurrent().suspend();
PortletInvocationResponse pir;
try {

//
ActionInvocation action = new ActionInvocation(new GateInPortletInvocationContext(this, window, lifeCycle));
PortletContent state = (PortletContent) window.state;
action.setClientContext(new GateInClientContext());
action.setPortalContext(new AbstractPortalContext());
action.setInstanceContext(new AbstractInstanceContext(window.name, AccessMode.READ_ONLY));
action.setWindowContext(new AbstractWindowContext(window.name));
action.setUserContext(new AbstractUserContext());
action.setSecurityContext(new AbstractSecurityContext(Context.getCurrentRequest()));
action.setRequest(Context.getCurrentRequest());
action.setResponse(Context.getCurrentResponse());
action.setTarget(state.portlet.getContext());
action.setMode(mode_);
action.setWindowState(windowState_);
action.setNavigationalState(ParametersStateString.create());
action.setInteractionState(interactionState != null ? ParametersStateString.create(interactionState) : null);
action.setPublicNavigationalState(window.computePublicParameters());

//
PortletInvoker invoker = portletManager.getInvoker();
pir = invoker.invoke(action);
} catch (PortletInvokerException e) {
return Response.error(e);
} finally {
clf.resume();
lifeCycle.resume();
}

//
Expand Down Expand Up @@ -196,30 +197,35 @@ public Response serveResource(WindowContext window, String id, Map<String, Strin
}

//
ResourceInvocation resource = new ResourceInvocation(new GateInPortletInvocationContext(this, window));
resource.setResourceId(id);
resource.setCacheLevel(cacheability);
resource.setClientContext(new GateInClientContext());
resource.setPortalContext(new AbstractPortalContext());
resource.setInstanceContext(new AbstractInstanceContext(window.name, AccessMode.READ_ONLY));
resource.setWindowContext(new AbstractWindowContext(window.name));
resource.setUserContext(new AbstractUserContext());
resource.setSecurityContext(new AbstractSecurityContext(Context.getCurrentRequest()));
resource.setRequest(Context.getCurrentRequest());
resource.setResponse(Context.getCurrentResponse());
resource.setTarget(state.portlet.getContext());
resource.setMode(mode);
resource.setWindowState(windowState);
resource.setNavigationalState(navigationalState);
resource.setResourceState(resourceState != null ? ParametersStateString.create(resourceState) : null);
resource.setPublicNavigationalState(window.computePublicParameters());

//
ContextLifeCycle lifeCycle = Request.getCurrent().suspend();
PortletInvocationResponse pir;
try {

//
ResourceInvocation resource = new ResourceInvocation(new GateInPortletInvocationContext(this, window, lifeCycle));
resource.setResourceId(id);
resource.setCacheLevel(cacheability);
resource.setClientContext(new GateInClientContext());
resource.setPortalContext(new AbstractPortalContext());
resource.setInstanceContext(new AbstractInstanceContext(window.name, AccessMode.READ_ONLY));
resource.setWindowContext(new AbstractWindowContext(window.name));
resource.setUserContext(new AbstractUserContext());
resource.setSecurityContext(new AbstractSecurityContext(Context.getCurrentRequest()));
resource.setRequest(Context.getCurrentRequest());
resource.setResponse(Context.getCurrentResponse());
resource.setTarget(state.portlet.getContext());
resource.setMode(mode);
resource.setWindowState(windowState);
resource.setNavigationalState(navigationalState);
resource.setResourceState(resourceState != null ? ParametersStateString.create(resourceState) : null);
resource.setPublicNavigationalState(window.computePublicParameters());

//
pir = portletManager.getInvoker().invoke(resource);
} catch (PortletInvokerException e) {
return Response.error(e);
} finally {
lifeCycle.resume();
}

//
Expand Down
Expand Up @@ -18,8 +18,6 @@
*/
package org.gatein.portal.page.spi.portlet;

import java.io.IOException;
import java.io.Writer;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.LinkedList;
Expand All @@ -28,21 +26,19 @@
import java.util.Map;

import javax.portlet.MimeResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import juzu.impl.request.ContextLifeCycle;
import juzu.impl.request.Request;
import org.gatein.common.net.media.MediaType;
import org.gatein.pc.api.ContainerURL;
import org.gatein.pc.api.Mode;
import org.gatein.pc.api.ParametersStateString;
import org.gatein.pc.api.PortletInvoker;
import org.gatein.pc.api.PortletInvokerException;
import org.gatein.pc.api.URLFormat;
import org.gatein.pc.api.invocation.RenderInvocation;
import org.gatein.pc.api.invocation.response.FragmentResponse;
import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
import org.gatein.pc.api.invocation.response.ResponseProperties;
import org.gatein.pc.api.spi.PortletInvocationContext;
import org.gatein.pc.api.state.AccessMode;
import org.gatein.pc.portlet.impl.spi.AbstractInstanceContext;
import org.gatein.pc.portlet.impl.spi.AbstractPortalContext;
Expand All @@ -58,58 +54,64 @@
/**
* @author Julien Viet
*/
class PortletRenderTask extends RenderTask implements PortletInvocationContext {
class PortletRenderTask extends RenderTask {

/** . */
private final WindowContext context;
private final PortletContentProvider provider;

/** . */
private final PortletInvoker invoker;
private final WindowContext windowContext;

/** . */
private final RenderInvocation invocation;
private final PortletInvoker invoker;

/** . */
private ContextLifeCycle lifeCycle;
private final PortletContent content;

/** . */
private final GateInPortletInvocationContext invocationContext;

PortletRenderTask(PortletContentProvider provider, WindowContext context) {

PortletContent pc = (PortletContent) context.state;
private final HttpServletRequest servletReq;

//
RenderInvocation invocation = new RenderInvocation(this);
invocation.setClientContext(new GateInClientContext());
invocation.setPortalContext(new AbstractPortalContext());
invocation.setInstanceContext(new AbstractInstanceContext(context.name, AccessMode.READ_ONLY));
invocation.setWindowContext(new AbstractWindowContext(context.name));
invocation.setUserContext(new AbstractUserContext());
invocation.setSecurityContext(new AbstractSecurityContext(Context.getCurrentRequest()));
invocation.setRequest(Context.getCurrentRequest());
invocation.setResponse(Context.getCurrentResponse());
invocation.setTarget(pc.portlet.getContext());
invocation.setMode(pc.mode != null ? pc.mode : Mode.VIEW);
invocation.setWindowState(pc.windowState != null ? pc.windowState : org.gatein.pc.api.WindowState.NORMAL);
invocation.setNavigationalState(pc.parameters != null ? ParametersStateString.create(pc.parameters) : null);
invocation.setPublicNavigationalState(context.computePublicParameters());
/** . */
private final HttpServletResponse servletResp;

//
this.context = context;
PortletRenderTask(PortletContentProvider provider, WindowContext windowContext) {
this.windowContext = windowContext;
this.invoker = provider.portletManager.getInvoker();
this.invocation = invocation;
this.invocationContext = new GateInPortletInvocationContext(provider, context);
this.provider = provider;
this.content = (PortletContent) windowContext.state;
this.servletReq = Context.getCurrentRequest();
this.servletResp = Context.getCurrentResponse();
}

@Override
public Result execute(Locale locale) {
Request request = Request.getCurrent();
lifeCycle = request.suspend();

//
ContextLifeCycle lifeCycle = Request.getCurrent().suspend();
PortletInvocationResponse response = null;
PortletInvokerException failure = null;
try {

//
GateInPortletInvocationContext context = new GateInPortletInvocationContext(provider, windowContext, lifeCycle);
RenderInvocation invocation = new RenderInvocation(context);
invocation.setClientContext(new GateInClientContext());
invocation.setPortalContext(new AbstractPortalContext());
invocation.setInstanceContext(new AbstractInstanceContext(windowContext.name, AccessMode.READ_ONLY));
invocation.setWindowContext(new AbstractWindowContext(windowContext.name));
invocation.setUserContext(new AbstractUserContext());
invocation.setSecurityContext(new AbstractSecurityContext(servletReq));
invocation.setRequest(servletReq);
invocation.setResponse(servletResp);
invocation.setTarget(content.portlet.getContext());
invocation.setMode(content.mode != null ? content.mode : Mode.VIEW);
invocation.setWindowState(content.windowState != null ? content.windowState : org.gatein.pc.api.WindowState.NORMAL);
invocation.setNavigationalState(content.parameters != null ? ParametersStateString.create(content.parameters) : null);
invocation.setPublicNavigationalState(windowContext.computePublicParameters());

//
response = invoker.invoke(invocation);

} catch (PortletInvokerException e) {
failure = e;
} finally {
Expand Down Expand Up @@ -146,7 +148,7 @@ public Result execute(Locale locale) {
}
String title = fragment.getTitle();
if (title == null) {
title = context.resolveTitle(locale);
title = windowContext.resolveTitle(locale);
}
result = new Result.Fragment(headers, headerTags, title, fragment.getContent());
} else {
Expand All @@ -155,43 +157,4 @@ public Result execute(Locale locale) {
}
return result;
}

@Override
public MediaType getResponseContentType() {
return invocationContext.getResponseContentType();
}

@Override
public String encodeResourceURL(String url) throws IllegalArgumentException {
return invocationContext.encodeResourceURL(url);
}

@Override
public void renderURL(Writer writer, ContainerURL containerURL, URLFormat format) throws IOException {
invocationContext.renderURL(writer, containerURL, format);
}

@Override
public String renderURL(ContainerURL containerURL, URLFormat format) {
Request current = Request.getCurrent();
if (current != null) {
ContextLifeCycle currentLF = current.suspend();
try {
return doRenderURL(containerURL, format);
} finally {
currentLF.resume();
}
} else {
return doRenderURL(containerURL, format);
}
}

private String doRenderURL(ContainerURL containerURL, URLFormat format) {
lifeCycle.resume();
try {
return invocationContext.renderURL(containerURL, format);
} finally {
Request.getCurrent().suspend();
}
}
}
Expand Up @@ -62,15 +62,24 @@ public static WebArchive createPortal() {
public void testRenderURL() throws Exception {
driver.get(deploymentURL.toString() + "/page1");
WebElement element = driver.findElement(By.id("index"));
Assert.assertTrue(element.getText().contains("hello"));
Assert.assertTrue(element.getText().contains("view"));
}

@Test
public void testAction() throws Exception {
driver.get(deploymentURL.toString() + "/page1");
WebElement element = driver.findElement(By.id("action"));
element.click();
Assert.assertTrue(driver.findElement(By.id("action")).getText().contains("world"));
Assert.assertTrue(driver.findElement(By.id("action")).getText().contains("action"));
}

@Test
public void testServeResource() throws Exception {
driver.get(deploymentURL.toString() + "/page1");
WebElement element = driver.findElement(By.id("resource"));
element.click();
Assert.assertTrue(driver.findElement(By.id("view")).getText().contains("view"));
Assert.assertTrue(driver.findElement(By.id("action")).getText().contains("action"));
}

@Test
Expand Down

0 comments on commit 2e8ab11

Please sign in to comment.