Skip to main content
summaryrefslogtreecommitdiffstats
blob: 35dd8259b75aede8aa534a7af7b7fc6c41d445fb (plain) (blame)
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
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * 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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.server.UnsecuredOseeHttpServlet;
import org.eclipse.osee.framework.core.services.IOseeCachingService;
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.jdk.core.util.Strings;
import org.eclipse.osee.framework.manager.servlet.data.ArtifactUtil;
import org.eclipse.osee.framework.manager.servlet.data.DefaultOseeArtifact;
import org.eclipse.osee.framework.manager.servlet.data.HttpArtifactFileInfo;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.framework.resource.management.IResourceLocator;
import org.eclipse.osee.framework.resource.management.IResourceManager;
import org.eclipse.osee.framework.resource.management.StandardOptions;
import org.eclipse.osee.logger.Log;
import org.osgi.framework.FrameworkUtil;

/**
 * @author Roberto E. Escobar
 */
public class ArtifactFileServlet extends UnsecuredOseeHttpServlet {

   private static final long serialVersionUID = -6334080268467740905L;

   private final IResourceManager resourceManager;
   private final BranchCache branchCache;

   private static Map<Integer, String> branchIdToGuidMap;

   public ArtifactFileServlet(Log logger, IResourceManager resourceManager, IOseeCachingService cachingService) {
      super(logger);
      this.resourceManager = resourceManager;
      this.branchCache = cachingService.getBranchCache();
   }

   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
      try {
         HttpArtifactFileInfo artifactFileInfo = null;

         String servletPath = request.getServletPath();
         //         System.out.println("servletPath: " + servletPath);
         if (!Strings.isValid(servletPath) || "/".equals(servletPath) || "/index".equals(servletPath)) {
            //            Enumeration<?> enumeration = request.getHeaderNames();
            //            while (enumeration.hasMoreElements()) {
            //               String headerField = (String) enumeration.nextElement();
            //               String value = request.getHeader(headerField);
            //               System.out.println(String.format("%s: %s", headerField, value));
            //            }

            Pair<String, String> defaultArtifact = DefaultOseeArtifact.get();
            if (defaultArtifact != null) {
               artifactFileInfo =
                  new HttpArtifactFileInfo(defaultArtifact.getFirst(), null, null, defaultArtifact.getSecond());
            }
         } else {
            artifactFileInfo = new HttpArtifactFileInfo(request);
         }

         String uri = null;
         if (artifactFileInfo != null) {
            Branch branch = null;
            if (artifactFileInfo.isBranchNameValid()) {
               branch = branchCache.getBySoleName(artifactFileInfo.getBranchName());
            } else if (artifactFileInfo.isBranchGuidValid()) {
               branch = branchCache.getByGuid(artifactFileInfo.getBranchGuid());
            } else {

               int branchId = artifactFileInfo.getId();
               if (branchId != HttpArtifactFileInfo.INVALID_BRANCH_ID) {
                  loadIdToGuidMapping();
                  branch = branchCache.getByGuid(branchIdToGuidMap.get(branchId));
               }

            }
            uri = ArtifactUtil.getUri(artifactFileInfo.getGuid(), branch);
         }
         handleArtifactUri(resourceManager, request.getQueryString(), uri, response);
      } catch (NumberFormatException ex) {
         handleError(response, HttpServletResponse.SC_BAD_REQUEST,
            String.format("Invalid Branch Id: [%s]", request.getQueryString()), ex);
      } catch (Exception ex) {
         handleError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
            String.format("Unable to acquire resource: [%s]", request.getQueryString()), ex);
      } finally {
         response.flushBuffer();
      }
   }

   public static void handleArtifactUri(IResourceManager resourceManager, String request, String uri, HttpServletResponse response) throws OseeCoreException {
      boolean wasProcessed = false;
      if (Strings.isValid(uri)) {
         IResourceLocator locator = resourceManager.getResourceLocator(uri);
         PropertyStore options = new PropertyStore();
         options.put(StandardOptions.DecompressOnAquire.name(), true);
         IResource resource = resourceManager.acquire(locator, options);

         if (resource != null) {
            wasProcessed = true;

            InputStream inputStream = null;
            try {
               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);
               if (!mimeType.equals("text/html")) {
                  response.setHeader("Content-Disposition", "attachment; filename=" + resource.getName());
               }
               Lib.inputStreamToOutputStream(inputStream, response.getOutputStream());
               response.flushBuffer();
            } catch (IOException ex) {
               OseeExceptions.wrapAndThrow(ex);
            } finally {
               Lib.close(inputStream);
            }
         }
      }
      if (!wasProcessed) {
         response.setStatus(HttpServletResponse.SC_NOT_FOUND);
         response.setContentType("text/plain");
         try {
            response.getWriter().write(String.format("Unable to find resource: [%s]", request));
         } catch (IOException ex) {
            OseeExceptions.wrapAndThrow(ex);
         }
      }
   }

   private void handleError(HttpServletResponse response, int status, String message, Throwable ex) throws IOException {
      response.setStatus(status);
      response.setContentType("text/plain");
      getLogger().error(ex, message);
      response.getWriter().write(Lib.exceptionToString(ex));
   }

   private synchronized void loadIdToGuidMapping() {
      if (branchIdToGuidMap == null) {
         branchIdToGuidMap = new HashMap<Integer, String>();
         BufferedReader reader = null;

         try {
            reader =
               new BufferedReader(new InputStreamReader(FrameworkUtil.getBundle(ArtifactFileServlet.class).getResource(
                  "templates/branchIdToBranchGuid.txt").openStream()));
            String branchIdAndGuidLine = null;

            while ((branchIdAndGuidLine = reader.readLine()) != null) {

               String[] idAndGuid = branchIdAndGuidLine.split(",");

               int branchId;
               try {
                  branchId = Integer.parseInt(idAndGuid[0]);
               } catch (Exception ex) {
                  branchId = -1;
               }

               branchIdToGuidMap.put(branchId, idAndGuid[1].trim());
            }
         } catch (Exception e) {
            getLogger().error(e, "Error mapping branchIds to guids");
         } finally {
            Lib.close(reader);
         }
      }
   }
}

Back to the top