chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f...@apache.org
Subject svn commit: r1204025 - in /chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench: ./ details/ model/ swing/
Date Sat, 19 Nov 2011 17:30:17 GMT
Author: fmui
Date: Sat Nov 19 17:30:17 2011
New Revision: 1204025

URL: http://svn.apache.org/viewvc?rev=1204025&view=rev
Log:
Workbench: added option to generate and verify content to the Create Document dialog

Added:
    chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/RandomInputStream.java
Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/CreateDocumentDialog.java
    chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/details/VersionTable.java
    chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientModel.java
    chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientSession.java
    chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/swing/IdRenderer.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/CreateDocumentDialog.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/CreateDocumentDialog.java?rev=1204025&r1=1204024&r2=1204025&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/CreateDocumentDialog.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/CreateDocumentDialog.java
Sat Nov 19 17:30:17 2011
@@ -19,23 +19,38 @@
 package org.apache.chemistry.opencmis.workbench;
 
 import java.awt.BorderLayout;
+import java.awt.Dimension;
 import java.awt.Frame;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
+import java.io.BufferedInputStream;
 import java.io.File;
+import java.io.InputStream;
+import java.text.NumberFormat;
 
+import javax.swing.Box;
+import javax.swing.BoxLayout;
 import javax.swing.JButton;
+import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
 import javax.swing.JFileChooser;
+import javax.swing.JFormattedTextField;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
 
+import org.apache.chemistry.opencmis.client.api.Document;
+import org.apache.chemistry.opencmis.client.api.ObjectId;
+import org.apache.chemistry.opencmis.commons.data.ContentStream;
 import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
 import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
 import org.apache.chemistry.opencmis.commons.enums.VersioningState;
 import org.apache.chemistry.opencmis.workbench.model.ClientModel;
+import org.apache.chemistry.opencmis.workbench.model.ClientSession;
 import org.apache.chemistry.opencmis.workbench.swing.CreateDialog;
 
 public class CreateDocumentDialog extends CreateDialog {
@@ -45,7 +60,10 @@ public class CreateDocumentDialog extend
     private JTextField nameField;
     private JComboBox typeBox;
     private JTextField filenameField;
+    private JFormattedTextField generateContentSizeField;
+    private JComboBox generateContentUnitField;
     private JComboBox versioningStateBox;
+    private JCheckBox verifyAfterUploadButton;
 
     public CreateDocumentDialog(Frame owner, ClientModel model) {
         this(owner, model, null);
@@ -81,6 +99,32 @@ public class CreateDocumentDialog extend
         JPanel filePanel = new JPanel(new BorderLayout());
 
         filenameField = new JTextField(30);
+        filenameField.getDocument().addDocumentListener(new DocumentListener() {
+            @Override
+            public void removeUpdate(DocumentEvent e) {
+                adjustGenerateContentComponents();
+            }
+
+            @Override
+            public void insertUpdate(DocumentEvent e) {
+                adjustGenerateContentComponents();
+            }
+
+            @Override
+            public void changedUpdate(DocumentEvent e) {
+            }
+
+            private void adjustGenerateContentComponents() {
+                if (filenameField.getText().length() == 0) {
+                    generateContentSizeField.setEnabled(true);
+                    generateContentUnitField.setEnabled(true);
+                } else {
+                    generateContentSizeField.setEnabled(false);
+                    generateContentUnitField.setEnabled(false);
+                }
+            }
+        });
+
         filePanel.add(filenameField, BorderLayout.CENTER);
 
         JButton browseButton = new JButton("Browse");
@@ -99,6 +143,25 @@ public class CreateDocumentDialog extend
 
         createRow("File:", filePanel, 2);
 
+        JPanel generateContentPanel = new JPanel();
+        generateContentPanel.setLayout(new BoxLayout(generateContentPanel, BoxLayout.X_AXIS));
+
+        generateContentSizeField = new JFormattedTextField(NumberFormat.getIntegerInstance());
+        generateContentSizeField.setValue(0L);
+        generateContentSizeField.setColumns(8);
+        generateContentSizeField.setHorizontalAlignment(JTextField.RIGHT);
+        generateContentSizeField.setMaximumSize(generateContentSizeField.getPreferredSize());
+        generateContentPanel.add(generateContentSizeField);
+
+        generateContentUnitField = new JComboBox(new String[] { "Bytes", "KiB", "MiB", "GiB"
});
+        generateContentUnitField.setMaximumSize(new Dimension((int) generateContentUnitField.getPreferredSize()
+                .getWidth() + 200, (int) generateContentUnitField.getPreferredSize().getHeight()));
+        generateContentPanel.add(generateContentUnitField);
+
+        generateContentPanel.add(Box.createHorizontalGlue());
+
+        createRow("Generate content:", generateContentPanel, 3);
+
         versioningStateBox = new JComboBox(new Object[] { VersioningState.NONE, VersioningState.MAJOR,
                 VersioningState.MINOR, VersioningState.CHECKEDOUT });
         if (((DocumentTypeDefinition) ((ObjectTypeItem) typeBox.getSelectedItem()).getObjectType()).isVersionable())
{
@@ -106,7 +169,10 @@ public class CreateDocumentDialog extend
         } else {
             versioningStateBox.setSelectedItem(VersioningState.NONE);
         }
-        createRow("Versioning State:", versioningStateBox, 3);
+        createRow("Versioning State:", versioningStateBox, 4);
+
+        verifyAfterUploadButton = new JCheckBox("Verify content after upload");
+        createRow("", verifyAfterUploadButton, 5);
 
         JButton createButton = new JButton("Create Document");
         createButton.addActionListener(new ActionListener() {
@@ -116,25 +182,57 @@ public class CreateDocumentDialog extend
                 String filename = filenameField.getText();
 
                 try {
-                    getClientModel().createDocument(name, type, filename,
-                            (VersioningState) versioningStateBox.getSelectedItem());
-                    getClientModel().reloadFolder();
+                    if (filename.length() > 0) {
+                        // create a document from a file
+                        ObjectId objectId = getClientModel().createDocument(name, type, filename,
+                                (VersioningState) versioningStateBox.getSelectedItem());
+
+                        if (verifyAfterUploadButton.isSelected()) {
+                            ContentStream contentStream = getClientModel().createContentStream(filename);
+                            verifyContentStreams(contentStream, objectId);
+                        }
+                    } else {
+                        // create a document with random data
+                        long seed = System.currentTimeMillis();
+                        long length = ((Number) generateContentSizeField.getValue()).longValue();
+                        if (length < 0) {
+                            length = 0;
+                        } else {
+                            for (int i = 0; i < generateContentUnitField.getSelectedIndex();
i++) {
+                                length = length * 1024;
+                            }
+                        }
+
+                        ObjectId objectId = getClientModel().createDocument(name, type, length,
seed,
+                                (VersioningState) versioningStateBox.getSelectedItem());
+
+                        if (verifyAfterUploadButton.isSelected()) {
+                            ContentStream contentStream = getClientModel().createContentStream("",
length, seed);
+                            verifyContentStreams(contentStream, objectId);
+                        }
+                    }
 
                     thisDialog.setVisible(false);
                     thisDialog.dispose();
                 } catch (Exception e) {
                     ClientHelper.showError(null, e);
+                } finally {
+                    try {
+                        getClientModel().reloadFolder();
+                    } catch (Exception e) {
+                        ClientHelper.showError(null, e);
+                    }
                 }
             }
         });
-        createRow("", createButton, 4);
+        createRow("", createButton, 6);
 
         if (file != null) {
             setFile(file);
         }
 
         getRootPane().setDefaultButton(createButton);
-        
+
         showDialog();
     }
 
@@ -144,4 +242,76 @@ public class CreateDocumentDialog extend
             nameField.setText(file.getName());
         }
     }
+
+    private void verifyContentStreams(ContentStream sourceContentStream, ObjectId objectId)
{
+        // download content from repository
+        ClientSession clientSession = getClientModel().getClientSession();
+        Document doc = (Document) clientSession.getSession().getObject(objectId,
+                clientSession.getObjectOperationContext());
+        ContentStream docContentStream = doc.getContentStream();
+
+        // compare
+        if (docContentStream == null) {
+            if (sourceContentStream.getLength() == 0) {
+                JOptionPane.showMessageDialog(getOwner(), "Source file and document content
are both empty.",
+                        "Verification successful", JOptionPane.INFORMATION_MESSAGE);
+            } else {
+                JOptionPane.showMessageDialog(getOwner(), "Document has no conent but the
source file is not empty!",
+                        "Verification failed", JOptionPane.ERROR_MESSAGE);
+            }
+            return;
+        }
+
+        InputStream sourceContent = null;
+        InputStream docContent = null;
+        try {
+            sourceContent = new BufferedInputStream(sourceContentStream.getStream());
+            docContent = new BufferedInputStream(docContentStream.getStream());
+
+            int fb = 0;
+            int db = 0;
+            long pos = 0;
+            while (fb > -1 && db > -1) {
+                fb = sourceContent.read();
+                db = docContent.read();
+
+                if (fb != db) {
+                    if (fb == -1) {
+                        JOptionPane.showMessageDialog(getOwner(),
+                                "The document content is bigger than the source file!", "Verification
failed",
+                                JOptionPane.ERROR_MESSAGE);
+                    } else if (db == -1) {
+                        JOptionPane.showMessageDialog(getOwner(),
+                                "The source file is bigger than the document content!", "Verification
failed",
+                                JOptionPane.ERROR_MESSAGE);
+                    } else {
+                        JOptionPane.showMessageDialog(getOwner(), "Contents differ at byte
" + pos + "!",
+                                "Verification failed", JOptionPane.ERROR_MESSAGE);
+                    }
+
+                    return;
+                }
+
+                pos++;
+            }
+
+            JOptionPane.showMessageDialog(getOwner(), "Source file and document content are
identical.",
+                    "Verification successful", JOptionPane.INFORMATION_MESSAGE);
+        } catch (Exception e) {
+            JOptionPane.showMessageDialog(getOwner(), "Content test exception: " + e.getMessage(),
+                    "Verification failed", JOptionPane.ERROR_MESSAGE);
+        } finally {
+            try {
+                sourceContent.close();
+            } catch (Exception e) {
+            }
+            try {
+                while (docContent.read() > -1) {
+                }
+
+                docContent.close();
+            } catch (Exception e) {
+            }
+        }
+    }
 }

Added: chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/RandomInputStream.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/RandomInputStream.java?rev=1204025&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/RandomInputStream.java
(added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/RandomInputStream.java
Sat Nov 19 17:30:17 2011
@@ -0,0 +1,95 @@
+/*
+ * 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.apache.chemistry.opencmis.workbench;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Random;
+
+public class RandomInputStream extends InputStream {
+
+    private final Random random;
+    private final long length;
+    private long pos;
+
+    public RandomInputStream(long length) {
+        this(length, System.currentTimeMillis());
+    }
+
+    public RandomInputStream(long length, long seed) {
+        random = new Random(seed);
+        this.length = length;
+        pos = 0;
+    }
+
+    @Override
+    public int available() throws IOException {
+        return (int) Math.min(Integer.MAX_VALUE, length - pos);
+    }
+
+    @Override
+    public long skip(long n) throws IOException {
+        if (n <= 0) {
+            return 0;
+        }
+
+        for (long l = 0; l < n; l++) {
+            if (read() == -1) {
+                return l;
+            }
+        }
+
+        return n;
+    }
+
+    @Override
+    public int read() throws IOException {
+        if (pos == length) {
+            return -1;
+        }
+
+        pos++;
+
+        return random.nextInt(256);
+    }
+
+    @Override
+    public int read(byte[] b) throws IOException {
+        return read(b, 0, b.length);
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        if (b == null) {
+            throw new NullPointerException();
+        } else if ((off < 0) || (len < 0) || (len > b.length - off)) {
+            throw new IndexOutOfBoundsException();
+        }
+
+        for (int i = 0; i < len; i++) {
+            int r = read();
+            if (r == -1) {
+                return i;
+            }
+            b[off + i] = (byte) r;
+        }
+
+        return len;
+    }
+}
\ No newline at end of file

Modified: chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/details/VersionTable.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/details/VersionTable.java?rev=1204025&r1=1204024&r2=1204025&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/details/VersionTable.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/details/VersionTable.java
Sat Nov 19 17:30:17 2011
@@ -71,7 +71,7 @@ public class VersionTable extends Abstra
                         try {
                             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                             List<Document> newVersions = doc.getAllVersions(getClientModel().getClientSession()
-                                    .geVersionOperationContext());
+                                    .getVersionOperationContext());
 
                             lock.writeLock().lock();
                             try {

Modified: chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientModel.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientModel.java?rev=1204025&r1=1204024&r2=1204025&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientModel.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientModel.java
Sat Nov 19 17:30:17 2011
@@ -18,6 +18,7 @@
  */
 package org.apache.chemistry.opencmis.workbench.model;
 
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
@@ -34,6 +35,7 @@ import org.apache.chemistry.opencmis.cli
 import org.apache.chemistry.opencmis.client.api.FileableCmisObject;
 import org.apache.chemistry.opencmis.client.api.Folder;
 import org.apache.chemistry.opencmis.client.api.ItemIterable;
+import org.apache.chemistry.opencmis.client.api.ObjectId;
 import org.apache.chemistry.opencmis.client.api.ObjectType;
 import org.apache.chemistry.opencmis.client.api.OperationContext;
 import org.apache.chemistry.opencmis.client.api.QueryResult;
@@ -50,6 +52,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.enums.VersioningState;
 import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
 import org.apache.chemistry.opencmis.commons.impl.MimeTypes;
+import org.apache.chemistry.opencmis.workbench.RandomInputStream;
 
 public class ClientModel {
 
@@ -220,7 +223,7 @@ public class ClientModel {
         ContentStream content = null;
         if ((filename != null) && (filename.length() > 0)) {
             File file = new File(filename);
-            InputStream stream = new FileInputStream(file);
+            InputStream stream = new BufferedInputStream(new FileInputStream(file));
 
             content = clientSession.getSession().getObjectFactory()
                     .createContentStream(file.getName(), file.length(), MimeTypes.getMIMEType(file),
stream);
@@ -229,23 +232,39 @@ public class ClientModel {
         return content;
     }
 
-    public synchronized void createDocument(String name, String type, String filename, VersioningState
versioningState)
-            throws Exception {
+    public synchronized ObjectId createDocument(String name, String type, String filename,
+            VersioningState versioningState) throws Exception {
         Map<String, Object> properties = new HashMap<String, Object>();
         properties.put(PropertyIds.NAME, name);
         properties.put(PropertyIds.OBJECT_TYPE_ID, type);
 
         ContentStream content = createContentStream(filename);
-        clientSession.getSession()
-                .createDocument(properties, currentFolder, content, versioningState, null,
null, null);
+        return clientSession.getSession().createDocument(properties, currentFolder, content,
versioningState, null,
+                null, null);
+    }
+
+    public ContentStream createContentStream(String name, long length, long seed) throws
Exception {
+        return clientSession.getSession().getObjectFactory()
+                .createContentStream(name, length, "application/octet-stream", new RandomInputStream(length,
seed));
+    }
+
+    public synchronized ObjectId createDocument(String name, String type, long length, long
seed,
+            VersioningState versioningState) throws Exception {
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put(PropertyIds.NAME, name);
+        properties.put(PropertyIds.OBJECT_TYPE_ID, type);
+
+        ContentStream content = createContentStream(name, length, seed);
+        return clientSession.getSession().createDocument(properties, currentFolder, content,
versioningState, null,
+                null, null);
     }
 
-    public synchronized void createFolder(String name, String type) throws Exception {
+    public synchronized ObjectId createFolder(String name, String type) throws Exception
{
         Map<String, Object> properties = new HashMap<String, Object>();
         properties.put(PropertyIds.NAME, name);
         properties.put(PropertyIds.OBJECT_TYPE_ID, type);
 
-        clientSession.getSession().createFolder(properties, currentFolder, null, null, null);
+        return clientSession.getSession().createFolder(properties, currentFolder, null, null,
null);
     }
 
     public synchronized List<ObjectType> getCreateableTypes(String rootTypeId) {

Modified: chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientSession.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientSession.java?rev=1204025&r1=1204024&r2=1204025&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientSession.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/model/ClientSession.java
Sat Nov 19 17:30:17 2011
@@ -199,7 +199,7 @@ public class ClientSession {
         return folderOperationContext;
     }
 
-    public OperationContext geVersionOperationContext() {
+    public OperationContext getVersionOperationContext() {
         return versionOperationContext;
     }
 

Modified: chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/swing/IdRenderer.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/swing/IdRenderer.java?rev=1204025&r1=1204024&r2=1204025&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/swing/IdRenderer.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-workbench/chemistry-opencmis-workbench/src/main/java/org/apache/chemistry/opencmis/workbench/swing/IdRenderer.java
Sat Nov 19 17:30:17 2011
@@ -46,7 +46,7 @@ public class IdRenderer extends DefaultT
         String text = "";
         if (value instanceof ObjectId) {
             if (((ObjectId) value).getId() != null) {
-                text = ((ObjectId) value).getId();
+                text = "<html><u>" + ((ObjectId) value).getId() + "</u></html>";
             }
         }
 



Mime
View raw message