1
2 package net.sourceforge.barbecue;
3
4 import javax.portlet.*;
5
6 import java.io.*;
7 import java.util.*;
8 import net.sourceforge.barbecue.output.*;
9
10 /**
11 *
12 * Barcode portlet
13 *
14 * Note: this portlet uses features from
15 * the Portlet 2.0 specification (JSR-286).
16 * This portlet will not run in a Portlet 1.0 container.
17 *
18 * @author Sean Sullivan
19 *
20 */
21 public class BarcodePortlet
22 extends GenericPortlet
23 implements ResourceServingPortlet
24
25 {
26 private static final String PARAM_BARCODE_DATA = "barcode_data";
27 private static final String SESSION_KEY = "barcode_data";
28
29 protected java.lang.String getTitle(RenderRequest request)
30 {
31 return "Barcode portlet";
32 }
33
34
35 protected void doEdit(RenderRequest req, RenderResponse resp)
36 throws PortletException, IOException
37 {
38 resp.setContentType("text/html");
39 PrintWriter writer = resp.getWriter();
40 PortletURL url = resp.createActionURL();
41 writer.println("<form method=\"POST\" action=\"" + url + "\">");
42 writer.println("Enter string: <input name=\"" + PARAM_BARCODE_DATA + "\" type=text size=30></input>");
43 writer.println("<input type=submit value=\"Submit\"></input>");
44 writer.println("</form>");
45
46 }
47
48 protected void doView(RenderRequest req, RenderResponse resp)
49 throws PortletException, IOException
50 {
51 resp.setContentType("text/html");
52 PrintWriter writer = resp.getWriter();
53 ResourceURL url = resp.createResourceURL();
54 writer.println("<img src=\"" + url + "\" />");
55 }
56
57 protected void doHelp(RenderRequest req, RenderResponse resp)
58 throws PortletException, IOException
59 {
60 resp.setContentType("text/html");
61 PrintWriter writer = resp.getWriter();
62 writer.println("<a target=\"_blank\" href=\"http://en.wikipedia.org/wiki/Barcode\">What is a barcode?</a>");
63 }
64
65 public void processAction(ActionRequest req,
66 ActionResponse resp)
67 throws PortletException,
68 java.io.IOException
69 {
70 storeDataInSession(req);
71 }
72
73 static private void storeDataInSession(PortletRequest req)
74 {
75 PortletSession session = req.getPortletSession(true);
76 session.setAttribute(SESSION_KEY, req.getParameter(PARAM_BARCODE_DATA));
77 }
78
79 static private Barcode createBarcode(String data)
80 {
81 if (data == null)
82 {
83 data = "Barcode";
84 }
85
86 try
87 {
88 Barcode b = BarcodeFactory.createCode128(data);
89 return b;
90 }
91 catch (BarcodeException ex)
92 {
93 throw new RuntimeException(ex);
94 }
95 }
96
97 static private Barcode createBarcode(PortletRequest req)
98 {
99 PortletSession sess = req.getPortletSession(true);
100 return createBarcode( (String) sess.getAttribute(SESSION_KEY));
101 }
102
103
104 public void serveResource(ResourceRequest req, ResourceResponse resp)
105 throws PortletException, IOException
106 {
107
108 Barcode b = createBarcode(req);
109
110 if (b == null)
111 {
112 b = createBarcode("Barcode");
113 }
114
115 resp.setContentType("image/png");
116 OutputStream out = resp.getPortletOutputStream();
117 try
118 {
119 BarcodeImageHandler.writePNG(b, out);
120 out.flush();
121 }
122 catch (OutputException ex)
123 {
124 throw new PortletException(ex);
125 }
126
127
128 }
129 }