01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.web.controller;
18:
19: import java.io.ByteArrayInputStream;
20: import java.io.File;
21: import java.io.FileOutputStream;
22:
23: import javax.servlet.ServletException;
24: import javax.servlet.http.HttpServletRequest;
25: import javax.servlet.http.HttpServletResponse;
26:
27: import org.apache.servicemix.jbi.framework.AdminCommandsServiceMBean;
28: import org.apache.servicemix.jbi.util.FileUtil;
29: import org.springframework.validation.BindException;
30: import org.springframework.web.bind.ServletRequestDataBinder;
31: import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
32: import org.springframework.web.servlet.ModelAndView;
33: import org.springframework.web.servlet.mvc.SimpleFormController;
34:
35: public class DeployServiceAssembly extends SimpleFormController {
36:
37: private final AdminCommandsServiceMBean adminCommandsService;
38:
39: public DeployServiceAssembly(
40: AdminCommandsServiceMBean adminCommandsService) {
41: this .adminCommandsService = adminCommandsService;
42: }
43:
44: protected ModelAndView onSubmit(HttpServletRequest request,
45: HttpServletResponse response, Object command,
46: BindException errors) throws Exception {
47: // cast the bean
48: FileUploadBean bean = (FileUploadBean) command;
49: // let's see if there's content there
50: byte[] file = bean.getFile();
51: if (file == null) {
52: // hmm, that's strange, the user did not upload anything
53: }
54: File f = File.createTempFile("smx-sa", ".zip");
55: try {
56: FileUtil.copyInputStream(new ByteArrayInputStream(file),
57: new FileOutputStream(f));
58: String result = adminCommandsService.deployServiceAssembly(
59: f.toURL().toString(), false);
60: System.err.println(result);
61: } finally {
62: f.delete();
63: }
64: // well, let's do nothing with the bean for now and return
65: return super .onSubmit(request, response, command, errors);
66: }
67:
68: protected void initBinder(HttpServletRequest request,
69: ServletRequestDataBinder binder) throws ServletException {
70: // to actually be able to convert Multipart instance to byte[]
71: // we have to register a custom editor
72: binder.registerCustomEditor(byte[].class,
73: new ByteArrayMultipartFileEditor());
74: // now Spring knows how to handle multipart object and convert them
75: }
76:
77: }
|