svn commit: r1690515 - in /ofbiz/trunk/specialpurpose/example: ./ config/ lib/ src/org/ofbiz/example/ webapp/example/WEB-INF/ widget/example/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1690515 - in /ofbiz/trunk/specialpurpose/example: ./ config/ lib/ src/org/ofbiz/example/ webapp/example/WEB-INF/ widget/example/

shijh
Author: shijh
Date: Sun Jul 12 19:46:54 2015
New Revision: 1690515

URL: http://svn.apache.org/r1690515
Log:
OFBIZ-6504.

Add an example to protect a pdf generated by FOP with owner password and user password.

Added:
    ofbiz/trunk/specialpurpose/example/config/examplepdf.properties   (with props)
    ofbiz/trunk/specialpurpose/example/lib/bcprov-jdk15-1.44.jar   (with props)
    ofbiz/trunk/specialpurpose/example/src/org/ofbiz/example/ExampleEvents.java   (with props)
Modified:
    ofbiz/trunk/specialpurpose/example/build.xml
    ofbiz/trunk/specialpurpose/example/config/ExampleUiLabels.xml
    ofbiz/trunk/specialpurpose/example/ofbiz-component.xml
    ofbiz/trunk/specialpurpose/example/webapp/example/WEB-INF/controller.xml
    ofbiz/trunk/specialpurpose/example/widget/example/CommonScreens.xml
    ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml
    ofbiz/trunk/specialpurpose/example/widget/example/ExampleScreens.xml

Modified: ofbiz/trunk/specialpurpose/example/build.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/build.xml?rev=1690515&r1=1690514&r2=1690515&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/example/build.xml (original)
+++ ofbiz/trunk/specialpurpose/example/build.xml Sun Jul 12 19:46:54 2015
@@ -51,5 +51,6 @@ under the License.
         <fileset dir="../../framework/webapp/lib" includes="*.jar"/>
         <fileset dir="../../framework/webapp/build/lib" includes="*.jar"/>
         <fileset dir="../../framework/common/build/lib" includes="*.jar"/>
+        <fileset dir="../../applications/content/lib" includes="*.jar"/>
     </path>
 </project>

Modified: ofbiz/trunk/specialpurpose/example/config/ExampleUiLabels.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/config/ExampleUiLabels.xml?rev=1690515&r1=1690514&r2=1690515&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/example/config/ExampleUiLabels.xml (original)
+++ ofbiz/trunk/specialpurpose/example/config/ExampleUiLabels.xml Sun Jul 12 19:46:54 2015
@@ -1108,4 +1108,20 @@
         <value xml:lang="zh">你修改了选择框中的值</value>
         <value xml:lang="zh-TW">你修改了選擇框中的值</value>
     </property>
+    <property key="ExamplePdfPassword">
+        <value xml:lang="en">PDF With Password</value>
+        <value xml:lang="zh">有密码的PDF</value>
+    </property>
+    <property key="PageTitleSetExamplePdfPassword">
+        <value xml:lang="en">Set PDF Protected By Password</value>
+        <value xml:lang="zh">设置有密码保护的PDF</value>
+    </property>
+    <property key="ExampleGeneratePDF">
+        <value xml:lang="en">Generate PDF</value>
+        <value xml:lang="zh">生成PDF</value>
+    </property>
+    <property key="password_did_not_match_confirm_password">
+        <value xml:lang="en">Password did not match confirm password.</value>
+        <value xml:lang="zh">密码与验证密码不一致。</value>
+    </property>
 </resource>

Added: ofbiz/trunk/specialpurpose/example/config/examplepdf.properties
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/config/examplepdf.properties?rev=1690515&view=auto
==============================================================================
--- ofbiz/trunk/specialpurpose/example/config/examplepdf.properties (added)
+++ ofbiz/trunk/specialpurpose/example/config/examplepdf.properties Sun Jul 12 19:46:54 2015
@@ -0,0 +1,24 @@
+###############################################################################
+# 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.
+###############################################################################
+
+# If Y, the default.pdf.owner.password will be used as owner password, the user input password will be used as user password;
+# if N, the user input password will be used as owner password and user password.
+use.default.pdf.owner.password=N
+
+default.pdf.owner.password=ofbiz

Propchange: ofbiz/trunk/specialpurpose/example/config/examplepdf.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: ofbiz/trunk/specialpurpose/example/lib/bcprov-jdk15-1.44.jar
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/lib/bcprov-jdk15-1.44.jar?rev=1690515&view=auto
==============================================================================
Binary file - no diff available.

Propchange: ofbiz/trunk/specialpurpose/example/lib/bcprov-jdk15-1.44.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: ofbiz/trunk/specialpurpose/example/ofbiz-component.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/ofbiz-component.xml?rev=1690515&r1=1690514&r2=1690515&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/example/ofbiz-component.xml (original)
+++ ofbiz/trunk/specialpurpose/example/ofbiz-component.xml Sun Jul 12 19:46:54 2015
@@ -30,6 +30,7 @@ under the License.
 
     <!-- load single or multiple external libraries -->
     <classpath type="jar" location="build/lib/*"/>
+    <classpath type="jar" location="lib/*"/>
 
     <!-- entity resources: model(s), eca(s), group, and data definitions -->
     <entity-resource type="model" reader-name="main" loader="main" location="entitydef/entitymodel.xml"/>

Added: ofbiz/trunk/specialpurpose/example/src/org/ofbiz/example/ExampleEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/src/org/ofbiz/example/ExampleEvents.java?rev=1690515&view=auto
==============================================================================
--- ofbiz/trunk/specialpurpose/example/src/org/ofbiz/example/ExampleEvents.java (added)
+++ ofbiz/trunk/specialpurpose/example/src/org/ofbiz/example/ExampleEvents.java Sun Jul 12 19:46:54 2015
@@ -0,0 +1,220 @@
+/*******************************************************************************
+ * 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.ofbiz.example;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.apps.Fop;
+import org.apache.fop.apps.MimeConstants;
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
+import org.apache.pdfbox.pdmodel.encryption.BadSecurityHandlerException;
+import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.GeneralException;
+import org.ofbiz.base.util.UtilCodec;
+import org.ofbiz.base.util.UtilHttp;
+import org.ofbiz.base.util.UtilProperties;
+import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.entity.Delegator;
+import org.ofbiz.entity.util.EntityUtilProperties;
+import org.ofbiz.webapp.view.ApacheFopWorker;
+import org.ofbiz.widget.renderer.FormStringRenderer;
+import org.ofbiz.widget.renderer.ScreenRenderer;
+import org.ofbiz.widget.renderer.ScreenStringRenderer;
+import org.ofbiz.widget.renderer.macro.MacroFormRenderer;
+import org.ofbiz.widget.renderer.macro.MacroScreenRenderer;
+import org.xml.sax.SAXException;
+
+import freemarker.template.TemplateException;
+
+
+public class ExampleEvents {
+
+    public static final String module = ExampleEvents.class.getName();
+    
+    public static final String examplePdfProperties = "examplepdf.properties";
+    
+    public static final boolean useExampleDefaultOwnerPassword = UtilProperties.getPropertyValue(examplePdfProperties, "use.default.pdf.owner.password", "N").equalsIgnoreCase("Y");
+
+    public static final String exampleDefaultOwnerPassword = UtilProperties.getPropertyValue(examplePdfProperties, "default.pdf.owner.password", "ofbiz");
+    
+    public static final String resourceExample = "ExampleUiLables";
+
+    /** Set password to the specified example and output the generated PDF.
+     *@param request The HTTPRequest object for the current request
+     *@param response The HTTPResponse object for the current request
+     *@return String specifying the exit status of this event
+     */
+    public static String setExamplePdfPassword(HttpServletRequest request, HttpServletResponse response) {
+        Delegator delegator = (Delegator) request.getAttribute("delegator");
+        Locale locale = UtilHttp.getLocale(request);
+        ServletContext servletContext = request.getSession().getServletContext();
+
+        Map<String, Object> requestParams = UtilHttp.getParameterMap(request);
+        String password = (String) requestParams.get("PASSWORD");
+        String confirmPassword = (String) requestParams.get("CONFIRM_PASSWORD");
+
+        if (UtilValidate.isEmpty(password) && UtilValidate.isEmpty(confirmPassword) && (UtilValidate.isEmpty(exampleDefaultOwnerPassword) || !useExampleDefaultOwnerPassword)) {
+         return "nopassword";
+        }
+        if (UtilValidate.isNotEmpty(password) && !password.equals(confirmPassword)) {
+         String errMsg = UtilProperties.getMessage(resourceExample, "password_not_equal_confirm_password", locale);
+            request.setAttribute("_ERROR_MESSAGE_", errMsg);
+            return "error";
+        }
+        
+        // get the original pdf
+        String screenLocation = "component://example/widget/example/ExampleScreens.xml";
+        String reportScreenName = "ExampleReport";
+
+        // render a screen to get the XML document
+        Writer reportWriter = new StringWriter();
+
+        try {
+            ScreenStringRenderer foScreenRenderer = new MacroScreenRenderer(EntityUtilProperties.getPropertyValue("widget", "screenfop.name", delegator), EntityUtilProperties.getPropertyValue("widget", "screenfop.screenrenderer", delegator));
+            FormStringRenderer formStringRenderer = new MacroFormRenderer(EntityUtilProperties.getPropertyValue("widget", "screenfop.formrenderer", delegator), request, response);
+            ScreenRenderer screens = new ScreenRenderer(reportWriter, null, foScreenRenderer);
+            screens.populateContextForRequest(request, response, servletContext);
+
+            // this is the object used to render forms from their definitions
+            screens.getContext().put("formStringRenderer", formStringRenderer);
+            screens.getContext().put("simpleEncoder", UtilCodec.getEncoder(EntityUtilProperties.getPropertyValue("widget", "screenfop.encoder", delegator)));
+
+            screens.render(screenLocation, reportScreenName);
+        } catch (GeneralException e) {
+            String errMsg = "General error rendering screen [" + screenLocation + "]: " + e.toString();
+            Debug.logError(e, errMsg, module);
+            request.setAttribute("_ERROR_MESSAGE_", errMsg);
+            return "error";
+        } catch (IOException e) {
+            String errMsg = "IO error rendering screen [" + screenLocation + "]: " + e.toString();
+            Debug.logError(e, errMsg, module);
+            request.setAttribute("_ERROR_MESSAGE_", errMsg);
+            return "error";
+        } catch (SAXException e) {
+            String errMsg = "SAX (XML parse) error rendering screen [" + screenLocation + "]: " + e.toString();
+            Debug.logError(e, errMsg, module);
+            request.setAttribute("_ERROR_MESSAGE_", errMsg);
+            return "error";
+        } catch (ParserConfigurationException e) {
+            String errMsg = "Parser configuration error rendering screen [" + screenLocation + "]: " + e.toString();
+            Debug.logError(e, errMsg, module);
+            request.setAttribute("_ERROR_MESSAGE_", errMsg);
+            return "error";
+        } catch (TemplateException e) {
+            String errMsg = "Freemarker template error rendering screen [" + screenLocation + "]: " + e.toString();
+            Debug.logError(e, errMsg, module);
+            request.setAttribute("_ERROR_MESSAGE_", errMsg);
+            return "error";
+ }
+
+        // set the input source (XSL-FO) and generate the PDF
+        StreamSource src = new StreamSource(new StringReader(reportWriter.toString()));
+
+        // create the output stream for the generation
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+        try {
+            Fop fop = ApacheFopWorker.createFopInstance(out, MimeConstants.MIME_PDF);
+            ApacheFopWorker.transform(src, null, fop);
+            out.flush();
+            out.close();
+        } catch (FOPException e) {
+            request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
+            Debug.logError(e, module);
+            return "error";
+        } catch (IOException e) {
+            request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
+            Debug.logError(e, module);
+            return "error";
+ }
+        
+        // parse the pdf with PDFBox
+        ByteArrayInputStream is = new ByteArrayInputStream(out.toByteArray());
+        PDDocument document;
+ try {
+ document = PDDocument.load(is);
+        int keyLength = 40;
+        AccessPermission ap = new AccessPermission();
+        String ownerPassword = exampleDefaultOwnerPassword;
+        if (UtilValidate.isEmpty(ownerPassword) || !useExampleDefaultOwnerPassword) {
+         ownerPassword = password;
+        }
+        StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPassword, password, ap);
+        spp.setEncryptionKeyLength(keyLength);
+        document.protect(spp);
+ } catch (IOException e) {
+            request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
+            Debug.logError(e, module);
+            return "error";
+ } catch (BadSecurityHandlerException e) {
+            request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
+            Debug.logError(e, module);
+            return "error";
+ } finally {
+            try {
+             if (is != null) {
+     is.close();
+             }
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+
+ out = new ByteArrayOutputStream();
+ try {
+ document.save(out);
+        // set the content type and length
+        response.setContentType(MimeConstants.MIME_PDF);
+        response.setContentLength(out.size());
+ out.flush();
+ out.close();
+        // write to the browser
+         response.getOutputStream().write(out.toByteArray());
+            response.getOutputStream().flush();
+ } catch (COSVisitorException e) {
+            request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
+            Debug.logError(e, module);
+         return "error";
+ } catch (IOException e) {
+            request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
+            Debug.logError(e, module);
+         return "error";
+ }
+
+        return "success";
+    }
+
+}

Propchange: ofbiz/trunk/specialpurpose/example/src/org/ofbiz/example/ExampleEvents.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/trunk/specialpurpose/example/webapp/example/WEB-INF/controller.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/webapp/example/WEB-INF/controller.xml?rev=1690515&r1=1690514&r2=1690515&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/example/webapp/example/WEB-INF/controller.xml (original)
+++ ofbiz/trunk/specialpurpose/example/webapp/example/WEB-INF/controller.xml Sun Jul 12 19:46:54 2015
@@ -225,6 +225,19 @@ under the License.
         <response name="success" type="view" value="ExamplePieChart"/>
     </request-map>
     
+    <!-- Pdf Report Options -->
+    <request-map uri="ExampleReportPdfOptions">
+        <security https="true" auth="true"/>
+        <response name="success" type="view" value="ExampleReportPdfOptions"/>
+    </request-map>
+    <request-map uri="setExamplePdfPassword">
+        <security https="true" auth="true"/>
+        <event type="java" path="org.ofbiz.example.ExampleEvents" invoke="setExamplePdfPassword"/>
+        <response name="success" type="none"/>
+        <response name="nopassword" type="view" value="ExampleReportPdf"/>
+        <response name="error" type="view" value="ExampleReportPdfOptions"/>
+    </request-map>
+    
     <!-- end of request mappings -->
 
     <!-- View Mappings -->
@@ -283,5 +296,6 @@ under the License.
         application/vnd.oasis.opendocument.presentation
      -->
 
+    <view-map name="ExampleReportPdfOptions" type="screen" page="component://example/widget/example/ExampleScreens.xml#ExampleReportPdfOptions"/>
     <!-- end of view mappings -->
 </site-conf>

Modified: ofbiz/trunk/specialpurpose/example/widget/example/CommonScreens.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/widget/example/CommonScreens.xml?rev=1690515&r1=1690514&r2=1690515&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/example/widget/example/CommonScreens.xml (original)
+++ ofbiz/trunk/specialpurpose/example/widget/example/CommonScreens.xml Sun Jul 12 19:46:54 2015
@@ -103,6 +103,10 @@ under the License.
                                                 <parameter param-name="exampleId"/>
                                                 <image url-mode="raw" src="${iconsLocation}/page_white_acrobat.png"/>
                                             </link>
+                                            <link target="ExampleReportPdfOptions" text="${uiLabelMap.ExamplePdfPassword}" style="buttontext">
+                                                <parameter param-name="exampleId"/>
+                                                <image url-mode="raw" src="${iconsLocation}/page_white_acrobat.png"/>
+                                            </link>
                                             <link target="help" text="${uiLabelMap.CommonHelp}" style="buttontext" target-window="_blank">
                                                 <parameter param-name="topic" value="ExampleEditHelp"/>
                                                 <image url-mode="raw" src="${iconsLocation}/help.png"/>

Modified: ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml?rev=1690515&r1=1690514&r2=1690515&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml (original)
+++ ofbiz/trunk/specialpurpose/example/widget/example/ExampleForms.xml Sun Jul 12 19:46:54 2015
@@ -311,4 +311,11 @@ under the License.
         </field>
         <field name="submitButton" title="${uiLabelMap.CommonAdd}"><submit button-type="button"/></field>
     </form>
+
+    <form name="SetExamplePdfPassword" type="single" target="setExamplePdfPassword" title="">
+        <field name="exampleId"><hidden/></field>
+        <field name="PASSWORD" title="${uiLabelMap.CommonPassword}"><password size="15" maxlength="250"/></field>
+        <field name="CONFIRM_PASSWORD" title="${uiLabelMap.CommonPassword}" tooltip="* ${uiLabelMap.CommonConfirm}"><password size="15" maxlength="250"/></field>
+        <field name="submitButton" title="${uiLabelMap.ExampleGeneratePDF}"><submit button-type="button"/></field>
+    </form>
 </forms>

Modified: ofbiz/trunk/specialpurpose/example/widget/example/ExampleScreens.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/example/widget/example/ExampleScreens.xml?rev=1690515&r1=1690514&r2=1690515&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/example/widget/example/ExampleScreens.xml (original)
+++ ofbiz/trunk/specialpurpose/example/widget/example/ExampleScreens.xml Sun Jul 12 19:46:54 2015
@@ -494,4 +494,21 @@ under the License.
             </widgets>
         </section>
     </screen>
+
+    <screen name="ExampleReportPdfOptions">
+        <section>
+            <actions>
+                <set field="titleProperty" value="PageTitleSetExamplePdfPassword"/>
+                <set field="tabButtonItem" value="EditExample"/>
+                <set field="exampleId" from-field="parameters.exampleId"/>
+            </actions>
+            <widgets>
+                <decorator-screen name="CommonExampleDecorator" location="${parameters.mainDecoratorLocation}">
+                    <decorator-section name="body">
+                        <include-form name="SetExamplePdfPassword" location="component://example/widget/example/ExampleForms.xml"/>
+                    </decorator-section>
+                </decorator-screen>
+            </widgets>
+        </section>
+    </screen>
 </screens>