Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4b24cac2006d5df4071d1a9ceb127812a99c832d (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
/*******************************************************************************
 * 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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.jdk.core.type.OseeCoreException;
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.Conditions;
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;

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

   private static final long serialVersionUID = -6334080268467740905L;

   private final IResourceManager resourceManager;
   private final BranchCache branchCache;

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

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

         String servletPath = request.getServletPath();
         if (!Strings.isValid(servletPath) || "/".equals(servletPath) || "/index".equals(servletPath)) {
            Pair<String, Long> defaultArtifact = DefaultOseeArtifact.get();
            if (defaultArtifact != null) {
               artifactFileInfo =
                  new HttpArtifactFileInfo(defaultArtifact.getFirst(), 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.isBranchUuidValid()) {
               branch = branchCache.getByGuid(artifactFileInfo.getBranchUuid());
            }
            Conditions.checkNotNull(branch, "branch", "Unable to determine branch");
            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));
   }

}

Back to the top