KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > WebHandler


1 /*
2  * @(#)WebHandler.java 1.6 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 import javax.jnlp.BasicService;
38 import javax.jnlp.ServiceManager;
39 import javax.jnlp.UnavailableServiceException;
40 import java.io.*;
41 import java.net.*;
42
43 public class WebHandler {
44     
45     
46     static private BasicService _bs = null;
47     
48     static public boolean isEnabled() {
49     return true;
50     }
51     
52     static public void publish(String JavaDoc txt) {
53     // Construct POST request (Heavily inspired from
54
// JavaWorld TIP 34)
55
URL url = getPublishURL();
56     
57     try {
58         URLConnection urlConn = url.openConnection();
59         // Let the run-time system (RTS) know that we want input.
60
urlConn.setDoInput (true);
61         // Let the RTS know that we want to do output.
62
urlConn.setDoOutput (true);
63         // No caching, we want the real thing.
64
urlConn.setUseCaches (false);
65         // Specify the content type.
66
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
67         
68         // Send POST output.
69
DataOutputStream printout = new DataOutputStream (urlConn.getOutputStream());
70         String JavaDoc content = "contents=" + URLEncoder.encode(txt);
71         printout.writeBytes(content);
72         printout.flush ();
73         printout.close ();
74         
75         // Get response data.
76
DataInputStream input = new DataInputStream (urlConn.getInputStream ());
77         String JavaDoc str;
78         while (null != ((str = input.readLine()))) {
79         System.out.println (str);
80         }
81         input.close ();
82     } catch(IOException ioe) {
83         ioe.printStackTrace(System.out);
84     }
85     };
86     
87     static public void show() {
88     initialize();
89     _bs.showDocument(getPublishURL());
90     };
91     
92     static private URL getPublishURL() {
93     String JavaDoc location = System.getProperty("jnlp.publish-url");
94     System.out.println(location);
95     try {
96         return new URL(location);
97     } catch(MalformedURLException mue) {
98         mue.printStackTrace(System.out);
99         return null;
100     }
101     }
102     
103     static private synchronized void initialize() {
104     if (_bs != null) return;
105     try {
106         _bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
107     } catch(UnavailableServiceException e) {
108             _bs = null;
109     }
110     }
111 }
112
Popular Tags