001: package test;
002:
003: import java.util.Enumeration;
004: import java.io.IOException;
005: import java.io.PrintWriter;
006: import java.io.InputStream;
007:
008: import javax.servlet.ServletException;
009:
010: import javax.portlet.GenericPortlet;
011: import javax.portlet.PortletException;
012: import javax.portlet.RenderRequest;
013: import javax.portlet.ActionRequest;
014: import javax.portlet.ActionResponse;
015: import javax.portlet.RenderResponse;
016: import javax.portlet.PortletContext;
017: import javax.portlet.PortletRequestDispatcher;
018: import javax.portlet.PortletConfig;
019: import javax.portlet.PortletMode;
020: import javax.portlet.PortletURL;
021: import javax.portlet.WindowState;
022:
023: public class HelloWorldPortlet extends GenericPortlet {
024:
025: private PortletContext pContext;
026:
027: public void init(PortletConfig config) throws PortletException {
028:
029: super .init(config);
030: pContext = config.getPortletContext();
031: }
032:
033: public void processAction(ActionRequest request,
034: ActionResponse response) throws PortletException {
035:
036: String mode = request.getParameter("nextMode");
037:
038: if (mode != null && mode.equals("VIEW")) {
039: response.setPortletMode(PortletMode.VIEW);
040: } else if (mode != null && mode.equals("EDIT")) {
041: response.setPortletMode(PortletMode.EDIT);
042: }
043:
044: String state = request.getParameter("nextState");
045: if (state != null && state.equals("MINIMIZED")) {
046: response.setWindowState(WindowState.MINIMIZED);
047: }
048:
049: request.setAttribute("testAttribute1", "testVal1");
050: request.setAttribute("testAttribute2", "testVal2");
051:
052: }
053:
054: public void doView(RenderRequest request, RenderResponse response)
055: throws PortletException {
056:
057: response.setContentType(request.getResponseContentType());
058: try {
059: PortletRequestDispatcher dispatcher = pContext
060: .getRequestDispatcher("/WEB-INF/doView.jsp");
061: dispatcher.include(request, response);
062: } catch (IOException e) {
063: throw new PortletException(
064: "HelloWorldPortlet.doView exception", e);
065: }
066:
067: PortletURL modeChangeURI = response.createActionURL();
068: modeChangeURI.setParameter("nextMode", "EDIT");
069:
070: PortletURL stateChangeURI = response.createActionURL();
071: stateChangeURI.setParameter("nextState", "MINIMIZED");
072:
073: String imgURL = response.encodeURL(request.getContextPath()
074: + "/productName.gif");
075:
076: try {
077: PrintWriter writer = response.getWriter();
078: writer.write("<p><p><a href=\"");
079: writer.write(modeChangeURI.toString());
080: writer.write("\">Go to EDIT</a><p><p><a href=\"");
081: writer.write(stateChangeURI.toString());
082: writer.write("\">Go to MINIMIZED</a>");
083:
084: writer.write("<p><p><a href=\"");
085: writer.write(imgURL);
086: writer.write("\">Click here to see the portal image</a>");
087:
088: } catch (IOException e) {
089: throw new PortletException(
090: "HelloWorldPortlet.doView exception", e);
091: }
092: }
093:
094: public void doEdit(RenderRequest request, RenderResponse response)
095: throws PortletException {
096:
097: PortletURL uri = response.createActionURL();
098: uri.setParameter("nextMode", "VIEW");
099: uri.setParameter("nextState", "MINIMIZED");
100:
101: Enumeration names = request.getAttributeNames();
102:
103: PortletURL renderURL = response.createRenderURL();
104:
105: response.setContentType(request.getResponseContentType());
106: try {
107: PrintWriter writer = response.getWriter();
108: writer.write("Hello Portlet Edit<p><p>");
109: writer.write("<a href=\"");
110: writer.write(uri.toString());
111: writer.write("\">Go back to VIEW & MINIMIZED</a><p>");
112: writer.write("===============================<p>");
113:
114: while (names.hasMoreElements()) {
115: String name = (String) names.nextElement();
116: Object value = request.getAttribute(name);
117: if (value instanceof String) {
118: writer.write(" " + name + " : " + value);
119: writer.write("<p>");
120: }
121: }
122: writer.write("<P>");
123:
124: writer.write("<a href=\"");
125: writer.write(renderURL.toString());
126: writer.write("\">Stay in Edit</a><p>");
127: } catch (IOException e) {
128: throw new PortletException(
129: "HelloWorldPortlet.doEdit exception", e);
130: }
131:
132: }
133:
134: public void doHelp(RenderRequest request, RenderResponse response)
135: throws PortletException {
136:
137: System.out.println("***** HelloWorld: doHelp");
138: PortletURL uri = response.createRenderURL();
139: uri.setParameter("nextMode", "HELP");
140:
141: response.setContentType(request.getResponseContentType());
142: try {
143: PrintWriter writer = response.getWriter();
144: writer.write("Hello Portlet Help<p><p>");
145: writer.write("<a href=\"");
146: writer.write(uri.toString());
147: writer.write("\">Click here should keep you in HELP</a>");
148: } catch (IOException e) {
149: System.out.println("***** HelloWorld thrown exception : "
150: + e);
151: throw new PortletException(
152: "HelloWorldPortlet.doHelp exception", e);
153: }
154: }
155:
156: }
|