Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2683a918b214b4a993f24c631d57891cd699fa43 (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
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others. 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: IBM Corporation - initial API and implementation
 ******************************************************************************/
package org.eclipse.equinox.internal.p2.tools;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
import org.osgi.service.http.*;

public class FileServerApplication implements IApplication {
	private Map resources = new HashMap(10);

	private static class FileSystemContext implements HttpContext {
		private String base;

		public FileSystemContext(String base) {
			this.base = base;
		}

		public String getMimeType(String name) {
			return null;
		}

		public URL getResource(String name) {
			try {
				return new URL(base + name);
			} catch (MalformedURLException e) {
				return null;
			}
		}

		public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
			return true;
		}
	}

	public Object start(IApplicationContext context) throws Exception {
		Map args = context.getArguments();
		initializeFromArguments((String[]) args.get("application.args"));
		registerResources(resources);
		return null;
	}

	private void registerResources(Map list) {
		HttpService http = (HttpService) ServiceHelper.getService(Activator.getContext(), HttpService.class.getName());
		for (Iterator i = resources.keySet().iterator(); i.hasNext();) {
			String key = (String) i.next();
			String value = (String) resources.get(key);
			try {
				http.registerResources(key, "/", new FileSystemContext(value));
			} catch (NamespaceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void stop() {
	}

	public void initializeFromArguments(String[] args) throws Exception {
		if (args == null)
			return;
		for (int i = 0; i < args.length; i++) {
			// check for args without parameters (i.e., a flag arg)
			//			if (args[i].equals("-raw"))
			//				raw = true;

			// check for args with parameters. If we are at the last argument or 
			// if the next one has a '-' as the first character, then we can't have 
			// an arg with a param so continue.
			if (i == args.length - 1 || args[i + 1].startsWith("-")) //$NON-NLS-1$
				continue;
			String arg = args[++i];

			if (args[i - 1].equalsIgnoreCase("-resource"))
				resources.put(arg, args[++i]);
		}
	}
}

Back to the top