summaryrefslogtreecommitdiffstats
blob: 90bb004c8a344895abe92bc757de42a9efffdc73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.manager.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.server.ISessionManager;
import org.eclipse.osee.framework.core.server.SecureOseeHttpServlet;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.manager.servlet.data.HttpRequestDecoder;
import org.eclipse.osee.framework.manager.servlet.data.ServletResourceBridge;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.framework.resource.management.IResourceLocator;
import org.eclipse.osee.framework.resource.management.IResourceLocatorManager;
import org.eclipse.osee.framework.resource.management.IResourceManager;
import org.eclipse.osee.framework.resource.management.exception.MalformedLocatorException;

/**
 * This class is responsible for managing server-side resources. The class accepts HTTP requests to perform uploads,
 * deletes, and gets from clients granting access to server-side managed resources.
 * 
 * @author Robeto E. Escobar
 */
public class ResourceManagerServlet extends SecureOseeHttpServlet {
   private static final long serialVersionUID = 3777506351978711657L;

   private final IResourceLocatorManager locatorManager;
   private final IResourceManager resourceManager;

   public ResourceManagerServlet(ISessionManager sessionManager, IResourceLocatorManager locatorManager, IResourceManager resourceManager) {
      super(sessionManager);
      this.locatorManager = locatorManager;
      this.resourceManager = resourceManager;
   }

   private void handleError(HttpServletResponse response, String message, Throwable ex) {
      OseeLog.log(this.getClass(), Level.SEVERE, message, ex);
      try {
         response.getWriter().println(message);
      } catch (IOException ex1) {
         OseeLog.log(this.getClass(), Level.SEVERE, message, ex);
      }
   }

   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      InputStream inputStream = null;
      try {
         Pair<String, Boolean> parameters = HttpRequestDecoder.fromGetRequest(request);
         String path = parameters.getFirst();
         boolean isCheckExistance = parameters.getSecond();
         PropertyStore options = HttpRequestDecoder.getOptions(request);

         IResourceLocator locator = locatorManager.getResourceLocator(path);

         if (isCheckExistance) {
            boolean exists = resourceManager.exists(locator);
            response.setStatus(exists ? HttpServletResponse.SC_OK : HttpServletResponse.SC_NOT_FOUND);
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().println(String.format("[%s] was %sfound.", path, exists ? "" : "not "));
         } else {
            IResource resource = resourceManager.acquire(locator, options);
            if (resource != null) {
               inputStream = resource.getContent();

               response.setStatus(HttpServletResponse.SC_OK);
               response.setContentLength(inputStream.available());
               response.setCharacterEncoding("ISO-8859-1");
               String mimeType = URLConnection.guessContentTypeFromStream(inputStream);
               if (mimeType == null) {
                  mimeType = URLConnection.guessContentTypeFromName(resource.getLocation().toString());
                  if (mimeType == null) {
                     mimeType = "application/*";
                  }
               }
               response.setContentType(mimeType);
               response.setHeader("Content-Disposition", "attachment; filename=" + resource.getName());

               Lib.inputStreamToOutputStream(inputStream, response.getOutputStream());
            } else {
               response.setStatus(HttpServletResponse.SC_NOT_FOUND);
               response.flushBuffer();
            }
         }
      } catch (MalformedLocatorException ex) {
         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
         response.setContentType("text/plain");
         response.setCharacterEncoding("UTF-8");
         handleError(response, String.format("Unable to locate resource: [%s]", request.getRequestURI()), ex);
      } catch (Exception ex) {
         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
         response.setContentType("text/plain");
         response.setCharacterEncoding("UTF-8");
         handleError(response, String.format("Unable to acquire resource: [%s]", request.getRequestURI()), ex);
      } finally {
         if (inputStream != null) {
            inputStream.close();
         }
      }
   }

   @Override
   protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      int result = HttpServletResponse.SC_BAD_REQUEST;
      try {
         String[] args = HttpRequestDecoder.fromPutRequest(request);
         PropertyStore options = HttpRequestDecoder.getOptions(request);

         IResourceLocator locator = locatorManager.generateResourceLocator(args[0], args[1], args[2]);
         IResource tempResource = new ServletResourceBridge(request, locator);

         IResourceLocator actualLocator = resourceManager.save(locator, tempResource, options);
         result = HttpServletResponse.SC_CREATED;
         response.setStatus(result);
         response.setContentType("text/plain");
         response.getWriter().write(actualLocator.toString());
      } catch (MalformedLocatorException ex) {
         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
         response.setContentType("text/plain");
         handleError(response,
            String.format("Unable to locate resource: [%s] - %s", request.getRequestURI(), ex.getLocalizedMessage()),
            ex);
      } catch (Exception ex) {
         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
         response.setContentType("text/plain");
         handleError(response, String.format("Error saving resource: [%s]", ex.getLocalizedMessage()), ex);
      }
   }

   @Override
   protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      int result = HttpServletResponse.SC_BAD_REQUEST;
      try {
         String path = HttpRequestDecoder.fromDeleteRequest(request);
         IResourceLocator locator = locatorManager.getResourceLocator(path);
         int status = IResourceManager.OK;
         //Activator.getInstance().getResourceManager().delete(locator);
         if (status == IResourceManager.OK) {
            result = HttpServletResponse.SC_ACCEPTED;
         } else {
            result = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
         }
         response.setStatus(result);
         response.setContentType("text/plain");
         response.getWriter().write("Deleted: " + locator.toString());
         response.flushBuffer();
      } catch (MalformedLocatorException ex) {
         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
         response.setContentType("text/plain");
         handleError(response, String.format("Unable to locate resource: [%s]", request.getRequestURI()), ex);
      } catch (Exception ex) {
         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
         response.setContentType("text/plain");
         handleError(response, String.format("Unable to delete resource: [%s]", request.getRequestURI()), ex);
      }
   }

   // TODO Allow for bulk loading of resources
   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      super.doPost(req, resp); // Remove this line once implemented

      Set<IResourceLocator> locators = new HashSet<IResourceLocator>();
      for (IResourceLocator locator : locators) {
         try {
            PropertyStore options = new PropertyStore();
            resourceManager.acquire(locator, options);
         } catch (OseeCoreException ex) {
         }
      }
   }
}