summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet321.java
blob: 5be5d4cd88945da03671cbbfe2e8d254e075dbbc (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
/*******************************************************************************
 * Copyright (c) 2000, 2009 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.swt.snippets;

/*
 * Browser example snippet: Examine request and response headers in a Mozilla Browser
 * 
 * IMPORTANT: For this snippet to work properly all of the requirements
 * for using JavaXPCOM in a stand-alone application must be satisfied
 * (see http://www.eclipse.org/swt/faq.php#howusejavaxpcom).
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.3
 */

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.mozilla.interfaces.*;
import org.mozilla.xpcom.Mozilla;

public class Snippet321 {

public static void main(String [] args) {
	Display display = new Display();
	Shell shell = new Shell(display);
	shell.setBounds(10,10,200,200);
	shell.setLayout(new FillLayout());
	Browser browser;
	try {
		browser = new Browser(shell, SWT.MOZILLA);
	} catch (SWTError e) {
		System.out.println("Could not instantiate Browser: " + e.getMessage());
		display.dispose();
		return;
	}

	nsIObserver observer = new nsIObserver() {
		public nsISupports queryInterface(String aIID) {
			if (aIID.equals(nsIObserver.NS_IOBSERVER_IID) || aIID.equals(nsIObserver.NS_ISUPPORTS_IID)) {
				return this;
			}
			return null;
		}
		public void observe(nsISupports subject, String topic, String data) {
			nsIHttpChannel channel = (nsIHttpChannel)subject.queryInterface(nsIHttpChannel.NS_IHTTPCHANNEL_IID);
			nsIRequest request = (nsIRequest)subject.queryInterface(nsIRequest.NS_IREQUEST_IID);
			if (topic.equals("http-on-modify-request")) {
				System.out.println("---------------------\nSome Request Header Values for " + request.getName() + ':');
				printRequestHeader(channel, "accept");
				printRequestHeader(channel, "accept-language");
				printRequestHeader(channel, "host");
				printRequestHeader(channel, "user-agent");
			} else {
				/* http-on-examine-response */
				System.out.println("---------------------\n\tSome Response Header Values for " + request.getName() + ':');
				printResponseHeader(channel, "content-length");
				printResponseHeader(channel, "content-type");
				printResponseHeader(channel, "expires");
				printResponseHeader(channel, "server");
			}
		}
		void printRequestHeader(nsIHttpChannel channel, String header) {
			try {
				System.out.println(header + '=' + channel.getRequestHeader(header));
			} catch (Exception e) {
				// the header did not exist, just continue
			}
		}
		void printResponseHeader(nsIHttpChannel channel, String header) {
			try {
				System.out.println('\t' + header + '=' + channel.getResponseHeader(header));
			} catch (Exception e) {
				// the header did not exist, just continue
			}
		}
	};
	nsIObserverService observerService = (nsIObserverService)Mozilla.getInstance().getServiceManager().getServiceByContractID(
		"@mozilla.org/observer-service;1", nsIObserverService.NS_IOBSERVERSERVICE_IID);
	observerService.addObserver(observer, "http-on-modify-request", false);
	observerService.addObserver(observer, "http-on-examine-response", false);

	browser.setUrl("http://www.eclipse.org");
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	display.dispose();
}

}