1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.snippets;
15 
16 /*
17  * Browser example snippet: Send custom headers and post data with HTTP requests
18  *
19  * For a list of all SWT example snippets see
20  * http://www.eclipse.org/swt/snippets/
21  *
22  * @since 3.6
23  */
24 import org.eclipse.swt.*;
25 import org.eclipse.swt.browser.*;
26 import org.eclipse.swt.layout.*;
27 import org.eclipse.swt.widgets.*;
28 
29 public class Snippet330 {
30 
31 	static int BUTTONS_PER_ROW = 1;
32 
main(String[] args)33 	public static void main(String[] args) {
34 
35 		Display display = new Display();
36 		Shell shell = new Shell(display);
37 		shell.setText("Snippet 330");
38 		shell.setLayout(new GridLayout(BUTTONS_PER_ROW, true));
39 
40 		final Browser browser;
41 		try {
42 			browser = new Browser(shell, SWT.NONE);
43 		} catch (SWTError e) {
44 			System.out.println("Could not instantiate Browser: " + e.getMessage());
45 			display.dispose();
46 			return;
47 		}
48 		GridData data = new GridData(GridData.FILL_BOTH);
49 		data.horizontalSpan = BUTTONS_PER_ROW;
50 		browser.setLayoutData(data);
51 
52 		{
53 			Button setHeaders = new Button(shell, SWT.PUSH);
54 			setHeaders.setText("Send custom headers");
55 			setHeaders.addListener(SWT.Selection,event ->
56 				browser.setUrl("http://www.ericgiguere.com/tools/http-header-viewer.html", null,
57 							new String[] { "User-agent: SWT Browser", "Custom-header: this is just a demo" }));
58 		}
59 
60 		{
61 			Button postTextPlain = new Button(shell, SWT.PUSH);
62 			postTextPlain.setText("Post data as 'text/plain'");
63 			postTextPlain.addListener(SWT.Selection, event -> browser.setUrl("http://httpbin.org/post",
64 					"Plain text passed as postData", new String[] { "content-type: text/plain" }));
65 		}
66 
67 		{
68 			Button postTextPlainUtf8 = new Button(shell, SWT.PUSH);
69 			postTextPlainUtf8.setText("Post data as 'text/plain' and specify encoding.");
70 			postTextPlainUtf8.addListener(SWT.Selection, event -> browser.setUrl("http://httpbin.org/post",
71 					"Plain text passed as postData", new String[] { "content-type: text/plain; charset=UTF-8" }));
72 		}
73 
74 		{
75 			Button postUrlEncoded = new Button(shell, SWT.PUSH);
76 			postUrlEncoded.setText("Post data with url encoding key=value");
77 			postUrlEncoded.addListener(SWT.Selection, event -> browser.setUrl("http://httpbin.org/post", "MyKey1=MyValue1&MyKey2=MyValue2",
78 					new String[] { "content-type: application/x-www-form-urlencoded" }));
79 		}
80 
81 		{
82 			Button postDataBugzilla = new Button(shell, SWT.PUSH);
83 			postDataBugzilla.setText("Send post data to bugzilla. url encoding is used as default");
84 			postDataBugzilla.addListener(SWT.Selection, event -> browser.setUrl(
85 					"https://bugs.eclipse.org/bugs/buglist.cgi",
86 					"emailassigned_to1=1&bug_severity=enhancement&bug_status=NEW&email1=platform-swt-inbox&emailtype1=substring",
87 					null));
88 		}
89 
90 		{
91 			Button postDataBugzillaLongQuery = new Button(shell, SWT.PUSH);
92 			postDataBugzillaLongQuery.setText("Send post data to bugzilla. Very slow response");
93 			postDataBugzillaLongQuery.addListener(SWT.Selection, event -> browser.setUrl(
94 					"https://bugs.eclipse.org/bugs/buglist.cgi",
95 					"emailassigned_to1=1&bug_severity=enhancement&bug_status=NEW",
96 					null));
97 		}
98 
99 		shell.setSize(1000, 1000);
100 		shell.open();
101 		while (!shell.isDisposed()) {
102 			if (!display.readAndDispatch())
103 				display.sleep();
104 		}
105 		display.dispose();
106 	}
107 
108 }