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

Commit

Permalink
Fix bugs with cache level not properly managed in resource serving
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Jul 31, 2013
1 parent 834491b commit 7412fb0
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 193 deletions.
18 changes: 14 additions & 4 deletions portal/web/src/main/java/org/gatein/portal/page/PageContext.java
Expand Up @@ -95,7 +95,7 @@ public NodeState create(NodeContext<NodeState, ElementState> context) {
String contentId = portletCustomization.getContentId();
NodeState window = new NodeState(context);
WindowContent windowState = contentProvider.getContent(contentId, window);
windows.put(windowState.getName(), windowState);
windows.put(window.context.getName(), windowState);
return window;
} else {
return new NodeState(context);
Expand Down Expand Up @@ -129,8 +129,8 @@ public PageContext(

//
LinkedHashMap<String, WindowContext> a = new LinkedHashMap<String, WindowContext>(builder.windows.size());
for (WindowContent state : builder.windows.values()) {
a.put(state.getName(), new WindowContext(state, this));
for (Map.Entry<String, WindowContent> window : builder.windows.entrySet()) {
a.put(window.getKey(), new WindowContext(window.getKey(), window.getValue(), this));
}

//
Expand All @@ -148,7 +148,7 @@ public Builder builder() {
// Clone the windows
for (Map.Entry<String, WindowContext> entry : windowMap.entrySet()) {
WindowContext window = entry.getValue();
builder.windows.put(window.state.getName(), window.state.copy());
builder.windows.put(window.name, window.state.copy());
}

//
Expand All @@ -174,6 +174,16 @@ public Map<QName, String[]> getParameters() {

//

public void encodeParameters(Phase.View.Dispatch dispatch) {
Map<QName, String[]> parameters = state.getParameters();
HashMap<String, String[]> a = new HashMap<String, String[]>(parameters.size());
for (Map.Entry<QName, String[]> b : parameters.entrySet()) {
a.put(b.getKey().getLocalPart(), b.getValue());
}
Encoder encoder = new Encoder(a);
dispatch.setParameter(WindowContext.ENCODING, "javax.portlet.p", encoder.encode());
}

public Phase.View.Dispatch getDispatch() {
Phase.View.Dispatch view = Controller_.index(state.path, null, null, null, null);
for (WindowContext w : windows) {
Expand Down
Expand Up @@ -19,28 +19,20 @@
package org.gatein.portal.page;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.locks.ReentrantLock;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import juzu.PropertyType;
import juzu.Response;
import juzu.impl.common.Tools;
import juzu.io.Chunk;
import juzu.io.ChunkBuffer;
import juzu.request.RequestContext;
import org.gatein.portal.layout.Layout;
import org.gatein.portal.page.spi.RenderTask;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
Expand Down Expand Up @@ -134,7 +126,7 @@ private void done(ReactiveWindow window, Result result) {
boolean send;
lock.lock();
try {
results.put(window.context.state.getName(), result);
results.put(window.context.name, result);
send = results.size() == windows.size();
} finally {
lock.unlock();
Expand Down
73 changes: 27 additions & 46 deletions portal/web/src/main/java/org/gatein/portal/page/WindowContext.java
Expand Up @@ -19,7 +19,6 @@
package org.gatein.portal.page;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

Expand All @@ -29,7 +28,6 @@
import juzu.impl.common.PercentCodec;
import juzu.io.Encoding;
import juzu.request.Phase;
import org.gatein.pc.api.cache.CacheLevel;
import org.gatein.portal.page.spi.RenderTask;
import org.gatein.portal.page.spi.WindowContent;

Expand Down Expand Up @@ -61,10 +59,14 @@ public void encodeQueryParamValue(CharSequence s, Appendable appendable) throws
/** The related page. */
public final PageContext page;

/** . */
public final String name;

/** The intrisic state. */
public final WindowContent state;

WindowContext(WindowContent state, PageContext page) {
WindowContext(String name, WindowContent state, PageContext page) {
this.name = name;
this.page = page;
this.state = state;
}
Expand Down Expand Up @@ -107,68 +109,47 @@ public Map<String, String[]> computePublicParameters() {
return publicParameters;
}

public Phase.View.Dispatch dispatchOf(
String phase,
String windowState,
String mode,
Map<String, String[]> parameters,
CacheLevel cacheability) {

//
Phase.View.Dispatch dispatch = Controller_.index(page.state.path, phase, state.getName(), windowState, mode);

// Encode according to cacheability
if (cacheability == CacheLevel.PORTLET) {
// Only encode this window
encode(dispatch);
} else if (cacheability == CacheLevel.PAGE) {

// Encode all windows
for (WindowContext w : page.windows) {
w.encode(dispatch);
}

// Encode page parameters
HashMap<String, String[]> a = new HashMap<String, String[]>(page.getParameters().size());
for (Map.Entry<QName, String[]> b : page.getParameters().entrySet()) {
a.put(b.getKey().getLocalPart(), b.getValue());
}
Encoder encoder = new Encoder(a);
dispatch.setParameter(ENCODING, "javax.portlet.p", encoder.encode());
}
/**
* Encode the state of this window in the dispatch object.
*
* @param dispatch the dispatch
*/
public void encode(Phase.View.Dispatch dispatch) {
encode(dispatch, state);
}

// Append provided parameters
if (parameters != null) {
for (Map.Entry<String, String[]> parameter : parameters.entrySet()) {
dispatch.setParameter(parameter.getKey(), parameter.getValue());
}
}
return dispatch;
/**
* Encode the provided state in the dispatch object for the current window
*
* @param dispatch the dispatch
* @param state the state to encode
*/
public void encode(Phase.View.Dispatch dispatch, WindowContent state) {
encode(dispatch, state.getParameters(), state.getWindowState(), state.getMode());
}

/**
* Encode the navigational state of the window in the dispatch object.
* Encode the provided state in the dispatch object for the current window
*
* @param dispatch the dispatch
* @param parameters the parameters
* @param windowState the window state
* @param mode the mode
*/
void encode(Phase.View.Dispatch dispatch) {
String name = state.getName();
String parameters = state.getParameters();
public void encode(Phase.View.Dispatch dispatch, String parameters, String windowState, String mode) {
if (parameters != null) {
dispatch.setParameter(WindowContext.ENCODING, "javax.portlet.p." + name, parameters);
}
String windowState = state.getWindowState();
if (windowState != null) {
dispatch.setParameter("javax.portlet.w." + name, windowState);
}
String mode = state.getMode();
if (mode != null) {
dispatch.setParameter("javax.portlet.m." + name, mode);
}
}

@Override
public String toString() {
return "WindowState[name=" + state.getName() + ",parameters=" + state.getParameters() + "]";
return "WindowState[name=" + name + ",parameters=" + state.getParameters() + "]";
}
}
Expand Up @@ -28,34 +28,32 @@
*
* @author Julien Viet
*/
public interface WindowContent {

String getName();
public abstract class WindowContent {

/**
* Resolve the title for the specified locale.
*
* @param locale the locale
* @return the title or null
*/
String resolveTitle(Locale locale);
public abstract String resolveTitle(Locale locale);

String getParameters();
public abstract String getParameters();

void setParameters(String s);
public abstract void setParameters(String s);

String getWindowState();
public abstract String getWindowState();

void setWindowState(String ws);
public abstract void setWindowState(String ws);

String getMode();
public abstract String getMode();

void setMode(String m);
public abstract void setMode(String m);

Map<String, String[]> computePublicParameters(Map<QName, String[]> parameters);
public abstract Map<String, String[]> computePublicParameters(Map<QName, String[]> parameters);

Iterable<Map.Entry<QName, String[]>> getPublicParametersChanges(Map<String, String[]> changes);
public abstract Iterable<Map.Entry<QName, String[]>> getPublicParametersChanges(Map<String, String[]> changes);

WindowContent copy();
public abstract WindowContent copy();

}
Expand Up @@ -33,6 +33,8 @@
import org.gatein.pc.api.URLFormat;
import org.gatein.pc.api.cache.CacheLevel;
import org.gatein.pc.api.spi.PortletInvocationContext;
import org.gatein.portal.page.Controller_;
import org.gatein.portal.page.Encoder;
import org.gatein.portal.page.PageContext;
import org.gatein.portal.page.WindowContext;

Expand Down Expand Up @@ -68,13 +70,13 @@ public String encodeResourceURL(String url) throws IllegalArgumentException {

@Override
public String renderURL(ContainerURL containerURL, URLFormat format) {
Phase.View.Dispatch view;
Phase.View.Dispatch dispatch;
if (containerURL instanceof RenderURL) {
RenderURL renderURL = (RenderURL) containerURL;
PageContext.Builder a = window.page.builder();

// Remove this nasty cast
PortletContent copy = (PortletContent) a.getWindow(state.getName());
PortletContent copy = (PortletContent) a.getWindow(window.name);

ParametersStateString ns = (ParametersStateString) renderURL.getNavigationalState();
if (ns != null) {
Expand All @@ -90,35 +92,78 @@ public String renderURL(ContainerURL containerURL, URLFormat format) {
if (changes.size() > 0) {
a.apply(window.getPublicParametersChanges(changes));
}
view = a.build().getDispatch();
dispatch = a.build().getDispatch();
} else if (containerURL instanceof ActionURL) {
ActionURL actionURL = (ActionURL) containerURL;
ParametersStateString is = (ParametersStateString) actionURL.getInteractionState();
Map<String, String[]> parameters = is != null ? is.getParameters() : null;
view = dispatchOf("action", containerURL.getWindowState(), containerURL.getMode(), parameters, CacheLevel.PAGE);
PageContext page = window.page;
String targetWindowState = containerURL.getWindowState() != null && !org.gatein.pc.api.WindowState.NORMAL.equals(containerURL.getWindowState()) ? containerURL.getWindowState().toString() : null;
String targetMode = containerURL.getMode() != null && !Mode.VIEW.equals(containerURL.getMode()) ? containerURL.getMode().toString() : null;
dispatch = Controller_.index(page.state.path, "action", window.name, targetWindowState, targetMode);

// Encode all windows
for (WindowContext w : page.windows) {
w.encode(dispatch);
}

// Encode page parameters
page.encodeParameters(dispatch);

//
if (is != null) {
for (Map.Entry<String, String[]> parameter : is.getParameters().entrySet()) {
dispatch.setParameter(parameter.getKey(), parameter.getValue());
}
}
} else {
ResourceURL resourceURL = (ResourceURL) containerURL;
CacheLevel level = resourceURL.getCacheability();
ParametersStateString rs = (ParametersStateString) resourceURL.getResourceState();
Map<String, String[]> parameters = rs != null ? rs.getParameters() : null;
view = window.dispatchOf("resource", state.getWindowState(), state.getMode(), parameters, level);
PageContext page = window.page;

//
dispatch = Controller_.index(page.state.path, "resource", window.name, state.getWindowState(), state.getMode());

//
if (level == CacheLevel.PORTLET || level == CacheLevel.PAGE) {

// Encode this window
String ww = window.state.getParameters();
if (ww == null) {
ww = new Encoder(PortletContent.NO_PARAMETERS).encode();
}
window.encode(dispatch, ww, window.state.getWindowState(), window.state.getMode());

//
if (level == CacheLevel.PAGE) {

// Encode all windows
for (WindowContext w : page.windows) {
if (w != window) {
w.encode(dispatch);
}
}

// Encode page parameters
page.encodeParameters(dispatch);
}
}

// Append provided parameters
if (parameters != null) {
for (Map.Entry<String, String[]> parameter : parameters.entrySet()) {
dispatch.setParameter(parameter.getKey(), parameter.getValue());
}
}

//
String id = resourceURL.getResourceId();
if (id != null) {
view.setParameter("javax.portlet.r", id);
dispatch.setParameter("javax.portlet.r", id);
}
}
return view.toString();
}

private Phase.View.Dispatch dispatchOf(
String phase,
org.gatein.pc.api.WindowState windowState,
Mode mode,
Map<String, String[]> parameters,
CacheLevel cacheability) {
String targetWindowState = windowState != null && !org.gatein.pc.api.WindowState.NORMAL.equals(windowState) ? windowState.toString() : null;
String targetMode = mode != null && !Mode.VIEW.equals(mode) ? mode.toString() : null;
return window.dispatchOf(phase, targetWindowState, targetMode, parameters, cacheability);
return dispatch.toString();
}

@Override
Expand Down

0 comments on commit 7412fb0

Please sign in to comment.