Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 JBoss Inc
   *
   * Licensed 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.gridcc.mce.mceworkflow.servlets;
 
 
 
Allows to download workflow files from the VCR, to retrieve their content and to save content to them. The following parameters must be passed in the HttpServletRequest:

Parameters:
operation (Mandatory) The type of operation to be performed. That is one among the following values: - "download", to download the whole specified workflow in the form of a .bpr archive - "retrieve", to retrieve the content of a specified workflow file as plain text - "save", to save content to the specified workflow file (returns "true" if the operation is successful, "false" otherwise) - "getFromURL", to retrieve a file from a remote URL
fullPath (Mandatory) The full path of the specified workflow on the server
fileName (Mandatory) A name identifing a workflow file on the server
workflowName (Required for "save" and "retrieve" operations) The name identifing the full workflow on the server
fileContent (Required only for "save" operations) The content to save in the specified file on the server
 
 public class WorkflowManagerServlet extends AssetFileServlet {
 
     private static final long serialVersionUID = 510l;
 
     @Inject @Preferred
     private RulesRepository rulesRepository;
 
     public void doPost(HttpServletRequest requestHttpServletResponse response)
             throws IOException {
         doGet(requestresponse);
     }
 
     public void doGet(HttpServletRequest requestHttpServletResponse response)
             throws IOException {
 
         String operation = request.getParameter("operation");
 
         if (operation != null) {

            
DOWNLOAD OPERATION *******************
 
             if (operation.equalsIgnoreCase("download")) {
                 this.downloadWorkflow(requestresponse);
             }

            
RETRIEVE OPERATION *******************
 
             else if (operation.equalsIgnoreCase("retrieve")) {
                 this.retrieveWorkflow(requestresponse);
             }

            
SAVE OPERATION *******************
 
             else if (operation.equalsIgnoreCase("save")) {
                 this.saveWorkflow(requestresponse);
             }

            
GET FROM URL OPERATION *******************
            // else if (operation.equalsIgnoreCase("getFromURL")) {
            // this.saveFileFromURL(request, response);
            // } else if (operation.equalsIgnoreCase("getStatus")) {
            // this.sendDummyMonitoring(request, response);
            // }
        }
    }
    private String getFullPath(String pathString workflowName,
            String extensionString fileName) {
        if (fileName.indexOf("wsdlCatalog") >= 0) {
            return path + workflowName + "/META-INF";
        } else {
            return getFullPath(pathworkflowNameextension);
        }
    }
    private String getFullPath(String pathString workflowName,
            String extension) {
        String fullPath = path;
        if (extension.equals("bpr")) {
            fullPath += workflowName + "/bpr";
        } else if (extension.equals("wsdl") || extension.equals("xsd")) {
            fullPath += workflowName + "/wsdl/" + workflowName + "/wsdl";
        } else if (extension.equals("xml")) {
            fullPath += workflowName + "/submission";
        } else if (extension.equals("pdd")) {
            fullPath += workflowName + "/META-INF/pdd/" + workflowName;
        }
        return fullPath;
    }
    private void downloadWorkflow(HttpServletRequest request,
            HttpServletResponse responsethrows IOException {
        String uuid = request.getParameter("uuid");
        if (uuid == null) {
            return;
        }
        try {
            processAttachmentDownload(uuidresponse);
        } catch (Exception e) {
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.print("<h2>Sorry, file cannot be found!</h2>");
            writer.close();
        }
    }
    private void retrieveWorkflow(HttpServletRequest request,
            HttpServletResponse responsethrows IOException {
        String uuid = request.getParameter("uuid");
        String fullPath = "";// request.getParameter("fullPath");
        String fileName = request.getParameter("fileName");
        String workflowName = request.getParameter("workflowName");
        if (fileName == null || workflowName == null || fullPath == null) {
            return;
        }
        String extension = fileName.substring(fileName.indexOf('.') + 1); // request.getParameter("extension");
        fullPath = this
                .getFullPath(fullPathworkflowNameextensionfileName);
        fullPath = fullPath + "/" + fileName;
        try {
            AssetItem item = .loadAssetByUUID(uuid);
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            final JarInputStream jis = new JarInputStream(item
                    .getBinaryContentAttachment());
            JarEntry entry;
            final byte[] buf = new byte[1024];
            int len;
            while ((entry = jis.getNextJarEntry()) != null) {
                if (entry.getName().equals(fullPath)) {
                    while ((len = jis.read(buf)) >= 0) {
                        out.write(buf, 0, len);
                    }
                    break;
                }
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    new ByteArrayInputStream(out.toByteArray())));
            String line = null;
            StringBuffer sb = new StringBuffer(4 * (9216));
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
            out.close();
            jis.close();
            reader.close();
            response.setContentType("text/plain");
            PrintWriter writer = response.getWriter();
            writer.print(sb);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.print("<h2>Sorry, that file cannot be found</h2>");
            writer.close();
        }
    }
    private void saveWorkflow(HttpServletRequest request,
            HttpServletResponse responsethrows IOException {
        String uuid = request.getParameter("uuid");
        String fullPath = ""// request.getParameter("fullPath");
        String fileName = request.getParameter("fileName");
        String workflowName = request.getParameter("workflowName");
        if (fileName == null || workflowName == null || fullPath == null) {
            return;
        }
        String extension = fileName.substring(fileName.indexOf('.') + 1);
        String fullPathToFolder = this.getFullPath(fullPathworkflowName,
                extensionfileName);
        fullPath = fullPathToFolder + "/" + fileName;
        AssetItem item = .loadAssetByUUID(uuid);
        InputStream in = item.getBinaryContentAttachment();
        String fileContent = request.getParameter("fileContent");
        ByteArrayOutputStream bais = new ByteArrayOutputStream();
        JarOutputStream jos = new JarOutputStream(bais);
        JarInputStream jis = null;
        try {
            if (in == null) {
                jos.putNextEntry(new JarEntry(fullPath));
                ByteArrayInputStream i = new ByteArrayInputStream(fileContent
                        .getBytes());
                int len = 0;
                byte[] copyBuf = new byte[1024];
                while (len != -1) {
                    len = i.read(copyBuf, 0, copyBuf.length);
                    if (len > 0) {
                        jos.write(copyBuf, 0, len);
                    }
                }
                i.close();
                jos.closeEntry();
            } else {
                jis = new JarInputStream(in);
                boolean found = false;
                JarEntry entry;
                while ((entry = jis.getNextJarEntry()) != null) {
                    jos.putNextEntry(new JarEntry(entry.getName()));
                    String name = entry.getName();
                    if (fullPath.equals(name)) {
                        found = true;
                        ByteArrayInputStream i = new ByteArrayInputStream(
                                fileContent.getBytes());
                        int len = 0;
                        byte[] copyBuf = new byte[1024];
                        while (len != -1) {
                            len = i.read(copyBuf, 0, copyBuf.length);
                            if (len > 0) {
                                jos.write(copyBuf, 0, len);
                            }
                        }
                        i.close();
                    } else {
                        int len = 0;
                        byte[] copyBuf = new byte[1024];
                        while (len != -1) {
                            len = jis.read(copyBuf, 0, copyBuf.length);
                            if (len > 0) {
                                jos.write(copyBuf, 0, len);
                            }
                        }
                    }
                    jis.closeEntry();
                    jos.closeEntry();
                }
                // If the file is not found, add it
                if (!found) {
                    jos.putNextEntry(new JarEntry(fullPath));
                    ByteArrayInputStream i = new ByteArrayInputStream(
                            fileContent.getBytes());
                    int len = 0;
                    byte[] copyBuf = new byte[1024];
                    while (len != -1) {
                        len = i.read(copyBuf, 0, copyBuf.length);
                        if (len > 0) {
                            jos.write(copyBuf, 0, len);
                        }
                    }
                    i.close();
                    jos.closeEntry();
                }
            }
            item.updateBinaryContentAttachment(new ByteArrayInputStream(bais
                    .toByteArray()));
            item.checkin("Updated " + fileName);
        } catch (IOException e) {
            throw new RulesRepositoryException(e);
        } finally {
            if (jis != null) {
                jis.close();
            }
            jos.close();
        }
    }
    private Collection<StringcreateWorkflow(String fullPath,
            String workflowName) {
        Collection<Stringlist = new ArrayList<String>();
        list.add(fullPath + "/" + workflowName);
        list.add(getFullPath(fullPathworkflowName"bpr"));
        list.add(getFullPath(fullPathworkflowName"wsdl"));
        list.add(getFullPath(fullPathworkflowName"pdd"));
        list.add(getFullPath(fullPathworkflowName"xml"));
        return list;
    }
New to GrepCode? Check out our FAQ X