Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6d6e16c3394c7559790d5b804284849a803e4a46 (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
/*******************************************************************************
 * 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.core.client.server;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osee.framework.core.client.internal.CoreClientActivator;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.ExtensionPoints;
import org.osgi.framework.Bundle;

/**
 * @author Roberto E. Escobar
 */
public class HttpResourceRequest implements IHttpMethod {

   private static HttpResourceRequest instance = new HttpResourceRequest();

   private HttpResourceRequest() {
   }

   public static HttpResourceRequest getInstance() {
      return instance;
   }

   @Override
   public void processRequest(HttpRequest httpRequest, HttpResponse httpResponse) {
      String urlRequest = httpRequest.getUrlRequest();
      URL url = findResource(urlRequest);
      if (url != null) {
         sendResource(url, httpResponse.getOutputStream());
      } else {
         httpResponse.outputStandardError(400, "Invalid Request: *" + urlRequest + "*");
      }
   }

   private void sendResource(URL url, OutputStream outputStream) {
      BufferedInputStream bis = null;
      try {
         bis = new BufferedInputStream(url.openStream());
         PrintStream ps = new PrintStream(outputStream, true, "UTF-8");
         byte[] buffer = new byte[1024];
         int count;
         while ((count = bis.read(buffer)) != -1) {
            ps.write(buffer, 0, count);
         }
      } catch (IOException ex) {
         OseeLog.log(CoreClientActivator.class, Level.SEVERE, "Error sending requested resource", ex);
      } finally {
         try {
            bis.close();
         } catch (IOException ex) {
            OseeLog.log(CoreClientActivator.class, Level.SEVERE, "Error closing stream", ex);
         }
      }
   }

   private URL findResource(String urlRequested) {
      URL resource = null;
      if (Strings.isValid(urlRequested) && urlRequested.endsWith("/") != true) {
         List<IConfigurationElement> elements =
            ExtensionPoints.getExtensionElements("org.eclipse.osee.framework.skynet.core.WebPage", "WebPageFolder");
         for (IConfigurationElement element : elements) {
            String resourceName = element.getAttribute("Path");
            String bundleName = element.getContributor().getName();

            if (Strings.isValid(bundleName) && Strings.isValid(resourceName)) {
               try {
                  Bundle bundle = Platform.getBundle(bundleName);
                  URL url = bundle.getEntry(resourceName + urlRequested);
                  if (url != null) {
                     resource = FileLocator.resolve(url);
                     break;
                  }
               } catch (Exception ex) {
                  throw new IllegalArgumentException(String.format("Unable to Load: [%s.%s]", bundleName, resourceName));
               }
            }
         }
      }
      return resource;
   }
}

Back to the top