Java tutorial
/* * The DecidR Development Team 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 de.decidr.workflowmodeleditor.server.servicetask; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashSet; import java.util.Set; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.vaadin.external.org.apache.commons.fileupload.FileItemIterator; import com.vaadin.external.org.apache.commons.fileupload.FileItemStream; import com.vaadin.external.org.apache.commons.fileupload.FileUploadException; import com.vaadin.external.org.apache.commons.fileupload.servlet.ServletFileUpload; import com.vaadin.external.org.apache.commons.fileupload.servlet.ServletRequestContext; import de.decidr.model.WSDLAnalyzer; import de.decidr.model.acl.permissions.FilePermission; import de.decidr.model.acl.permissions.FileReadPermission; import de.decidr.model.acl.roles.UserRole; import de.decidr.model.entities.File; import de.decidr.model.exceptions.TransactionException; import de.decidr.model.facades.FileFacade; import de.decidr.workflowmodeleditor.client.servicetask.WSDLAnalyzeResult; import de.decidr.workflowmodeleditor.client.servicetask.WSDLUploadServlet; /** * Implementation of the GWT RPC interface for the workflow editor widget, * primarily used by the service task property dialog. * * @author Wolfgang Fellger */ public class WSDLUploadServletImpl extends RemoteServiceServlet implements WSDLUploadServlet { private static final long serialVersionUID = 7149591696714456580L; private UserRole callingUser; /** * Completely read input stream to memory and convert it to a ByteArrayInputStream. */ private static ByteArrayInputStream receiveUpload(InputStream stream) throws IOException { byte[] buffer = new byte[4096]; ByteArrayOutputStream data = new ByteArrayOutputStream(stream.available()); int bytesRead; while ((bytesRead = stream.read(buffer)) > 0) { data.write(buffer, 0, bytesRead); } return new ByteArrayInputStream(data.toByteArray()); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* Make sure that session is valid and field set (by WebPortal) */ if (request.getSession().getAttribute("authenticatedUser") == null) { response.getWriter().write("Invalid authentication!"); return; } else { callingUser = new UserRole((Long) request.getSession().getAttribute("authenticatedUser")); } boolean isMultipart = ServletFileUpload.isMultipartContent(new ServletRequestContext(request)); if (isMultipart) { FileItemStream uploadItem = getFileItem(request); if (uploadItem == null) { super.service(request, response); return; } response.setContentType("text/plain"); FileFacade ff = new FileFacade(callingUser); InputStream data = receiveUpload(uploadItem.openStream()); Set<Class<? extends FilePermission>> publicPermissions = new HashSet<Class<? extends FilePermission>>(); publicPermissions.add(FileReadPermission.class); try { Long fileID = ff.createFile(data, (long) data.available(), uploadItem.getName(), uploadItem.getContentType(), false, publicPermissions); response.getWriter().write(fileID.toString()); } catch (TransactionException e) { e.printStackTrace(); response.getWriter().write("Failed due to transaction exception!"); } } else { super.service(request, response); } } private FileItemStream getFileItem(HttpServletRequest request) { ServletFileUpload upload = new ServletFileUpload(); upload.setHeaderEncoding(request.getCharacterEncoding()); upload.setSizeMax(10000000); try { FileItemIterator items = upload.getItemIterator(request); while (items.hasNext()) { FileItemStream item = items.next(); if (!item.isFormField()) { return item; } } } catch (FileUploadException e) { return null; } catch (IOException e) { e.printStackTrace(); } return null; } @Override public void deleteFile(String fileID) { try { new FileFacade(callingUser).deleteFile(Long.parseLong(fileID)); } catch (Exception e) { System.err.println("Deleting file " + fileID + " failed:\n" + e.getMessage()); } } /* (non-Javadoc) * @see de.decidr.workflowmodeleditor.client.servicetask.WSDLUploadServlet#getFileDescription(java.lang.String) */ @Override public String getFileDescription(String fileID) throws Exception { File fileInfo = new FileFacade(callingUser).getFileInfo(Long.parseLong(fileID)); return fileInfo.getFileName() + " (" + fileInfo.getFileSizeBytes() / 1024 + " kB)"; } /* (non-Javadoc) * @see de.decidr.workflowmodeleditor.client.servicetask.WSDLUploadServlet#analyzeWSDL(java.lang.String) */ @Override public WSDLAnalyzeResult analyzeWSDL(String fileID) throws Exception { Long file = Long.parseLong(fileID); WSDLAnalyzer analyzer = new WSDLAnalyzer(file, callingUser); WSDLAnalyzeResult result = new WSDLAnalyzeResult(); result.operations = analyzer.getOperations(); result.porttypes = analyzer.getPortTypes(); result.endpoints = analyzer.getEndpoints(); return result; } }