View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.web.spring;
17  
18  import java.io.ByteArrayInputStream;
19  import java.text.SimpleDateFormat;
20  import java.util.Date;
21  import java.util.Iterator;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.http.HttpSession;
27  
28  import org.jdiagnose.RemoteResult;
29  import org.jdiagnose.remote.RemoteResultStore;
30  import org.jdiagnose.remote.file.ResultImporter;
31  import org.jdiagnose.remote.provider.DefaultSystemHolder;
32  import org.jdiagnose.remote.provider.MutableSystemProvider;
33  import org.jdiagnose.remote.system.InMemoryRemoteSystem;
34  import org.springframework.validation.BindException;
35  import org.springframework.web.bind.ServletRequestDataBinder;
36  import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
37  import org.springframework.web.servlet.ModelAndView;
38  import org.springframework.web.servlet.mvc.SimpleFormController;
39  import org.springframework.web.servlet.view.RedirectView;
40  
41  /***
42   * @author jmccrindle
43   */
44  public class ImportController extends SimpleFormController {
45      
46      private static final org.apache.log4j.Category log = org.apache.log4j.Category
47              .getInstance(ImportController.class);
48      
49      private SimpleDateFormat systemDateFormat = new SimpleDateFormat("HH:mm:ss");
50      private ResultImporter importer = null;
51      private MutableSystemProvider provider = null;
52      private RemoteResultStore resultStore = null;
53      private String sessionSystemsKey = "sessionSystemsKey";
54      private SessionSystemsFactory sessionSystemsFactory = null;
55  
56  
57      /* (non-Javadoc)
58       * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
59       */
60      protected Object formBackingObject(HttpServletRequest request)
61              throws Exception {
62          ImportCommand importCommand = new ImportCommand();
63          importCommand.setSystem("Import at " + systemDateFormat.format(new Date()));
64          importCommand.setRemoteSystem(true);
65          return importCommand;
66      }
67  
68      protected ModelAndView onSubmit(HttpServletRequest request,
69              HttpServletResponse response, Object command, BindException errors)
70              throws Exception {
71  
72          // cast the bean
73          ImportCommand importCommand = (ImportCommand) command;
74  
75          // let's see if there's content there
76          byte[] file = importCommand.getFile();
77          if (file == null) {
78              // hmm, that's strange, the user did not upload anything
79          } else {
80              Iterator results = importer.getResults(new ByteArrayInputStream(file));
81              InMemoryRemoteSystem remoteSystem = null; 
82              String systemName = importCommand.getSystem();
83              HttpSession session = request.getSession(true);
84              if(importCommand.isPersist() && importCommand.isRemoteSystem()) {
85                  remoteSystem = new InMemoryRemoteSystem();
86                  while (results.hasNext()) {
87                      RemoteResult result = (RemoteResult) results.next();
88                      remoteSystem.addRemoteResult(result);
89                      resultStore.addRemoteResult(result);
90                  }
91                  String systemHolderId = addRemoteSystem(remoteSystem, systemName);
92                  addToSession(session, systemHolderId);
93                  return new ModelAndView(new RedirectView(request.getContextPath() + "/hosts.htm?system=" + systemHolderId));
94              } else if(importCommand.isRemoteSystem()) {
95                  remoteSystem = new InMemoryRemoteSystem();
96                  remoteSystem.addRemoteResults(results);
97                  String systemHolderId = addRemoteSystem(remoteSystem, systemName);
98                  addToSession(session, systemHolderId);
99                  return new ModelAndView(new RedirectView(request.getContextPath() + "/hosts.htm?system=" + systemHolderId));
100             } else if(importCommand.isPersist()) {
101                 resultStore.addRemoteResults(results);
102                 return new ModelAndView(new RedirectView(request.getContextPath() + "/hosts.htm"));
103             }
104         }
105         return new ModelAndView(getFormView());
106     }
107     
108     private void addToSession(HttpSession session, String id) {
109         SessionSystems sessionSystems = (SessionSystems) session.getAttribute(sessionSystemsKey);
110         if(sessionSystems == null) {
111             sessionSystems = sessionSystemsFactory.create();
112             session.setAttribute(sessionSystemsKey, sessionSystems);
113         }
114         sessionSystems.getIds().add(id);
115     }
116 
117     /***
118      * @param remoteSystem
119      * @param systemName
120      */
121     private String addRemoteSystem(InMemoryRemoteSystem remoteSystem, String systemName) {
122         String systemHolderId = provider.nextId();
123         DefaultSystemHolder holder = new DefaultSystemHolder();
124         holder.setId(systemHolderId);
125         holder.setDescription(systemName);
126         holder.setRemoteSystem(remoteSystem);
127         provider.addHolder(holder);
128         return systemHolderId;
129     }
130 
131     protected void initBinder(HttpServletRequest request,
132             ServletRequestDataBinder binder) throws ServletException {
133         // to actually be able to convert Multipart instance to byte[]
134         // we have to register a custom editor (in this case the
135         // ByteArrayMultipartEditor
136         binder.registerCustomEditor(byte[].class,
137                 new ByteArrayMultipartFileEditor());
138         // now Spring knows how to handle multipart object and convert them
139     }
140 
141     public void setImporter(ResultImporter importer) {
142         this.importer = importer;
143     }
144     public void setSystemDateFormat(SimpleDateFormat systemDateFormat) {
145         this.systemDateFormat = systemDateFormat;
146     }
147     public void setProvider(MutableSystemProvider provider) {
148         this.provider = provider;
149     }
150     public void setResultStore(RemoteResultStore resultStore) {
151         this.resultStore = resultStore;
152     }
153     public void setSessionSystemsFactory(
154             SessionSystemsFactory sessionSystemsFactory) {
155         this.sessionSystemsFactory = sessionSystemsFactory;
156     }
157     public void setSessionSystemsKey(String sessionSystemsKey) {
158         this.sessionSystemsKey = sessionSystemsKey;
159     }
160 }
161