?? pushletclient.java
字號:
// Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
// Distributable under LGPL license. See terms of license at gnu.org.
package nl.justobjects.pushlet.client;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventParser;
import nl.justobjects.pushlet.core.Protocol;
import nl.justobjects.pushlet.util.PushletException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.*;
import java.util.Map;
/**
* Client API for Java HTTP client applets or apps.
* <p/>
* Use this class within Java client applications or applets.
* Implement a PushletClientListener to receive callbacks for
* data-related Event objects pushed by the server.
*
* @author Just van den Broecke - Just Objects ©
* @version $Id: PushletClient.java,v 1.18 2007/11/10 13:52:47 justb Exp $
* @see PushletClientListener
* @see nl.justobjects.pushlet.test.PushletApplet
* @see nl.justobjects.pushlet.test.PushletPingApplication
*/
public class PushletClient implements Protocol {
/**
* Pushlet URL.
*/
private String pushletURL;
/**
* Debug flag for verbose output.
*/
private boolean debug;
/**
* Id gotten on join ack
*/
private String id;
/**
* Internal listener for data events pushed by server.
*/
private DataEventListener dataEventListener;
/**
* Constructor with full pushlet URL.
*/
public PushletClient(String aPushletURL) {
pushletURL = aPushletURL;
}
/**
* Constructor with host and port using default URI.
*/
public PushletClient(String aHost, int aPort) {
this("http://" + aHost + ":" + aPort + DEFAULT_SERVLET_URI);
}
/**
* Set proxy options and optional proxy authentication.
* <p/>
* Contributed by Dele Olajide
* See http://groups.yahoo.com/group/pushlet/message/634
* <p/>
* Usage:
* PushletClient pushletClient = new PushletClient("http:://www.domain.com/pushlet");
* pushletClient.setProxyOptions("proxy.bla.com", "8080", ....);
* <p/>
* use pushletClient further as normal
*/
public void setProxyOptions(String aProxyHost,
String aProxyPort, String theNonProxyHosts,
String aUserName, String aPassword, String anNTLMDomain) {
// Enable proxying
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", aProxyHost);
System.setProperty("http.proxyPort", aProxyPort);
// Set optional non-proxy hosts
if (theNonProxyHosts != null) {
System.setProperty("http.nonProxyHosts", theNonProxyHosts);
}
// If user name specified configure proxy authentication
if (aUserName != null) {
System.setProperty("http.proxyUser", aUserName);
System.setProperty("http.proxyPassword", aPassword);
// See inner class below
Authenticator.setDefault(new HTTPAuthenticateProxy(aUserName, aPassword));
// Optional NT domain
if (anNTLMDomain != null) {
System.setProperty("http.auth.ntlm.domain", anNTLMDomain);
}
}
}
/**
* Join server, starts session.
*/
public void join() throws PushletException {
Event event = new Event(E_JOIN);
event.setField(P_FORMAT, FORMAT_XML);
Event response = doControl(event);
throwOnNack(response);
p("PushletClient join()");
// Join Ack received
id = response.getField(P_ID);
}
/**
* Leave server, stops session.
*/
public void leave() throws PushletException {
stopListen();
throwOnInvalidSession();
Event event = new Event(E_LEAVE);
event.setField(P_ID, id);
Event response = doControl(event);
throwOnNack(response);
id = null;
}
/**
* Open data channel.
*/
public void listen(PushletClientListener aListener) throws PushletException {
listen(aListener, MODE_STREAM);
}
/**
* Open data channel in stream or push mode.
*/
public void listen(PushletClientListener aListener, String aMode) throws PushletException {
listen(aListener, aMode, null);
}
/**
* Open data channel in stream or push mode with a subject.
*/
public void listen(PushletClientListener aListener, String aMode, String aSubject) throws PushletException {
throwOnInvalidSession();
stopListen();
String listenURL = pushletURL
+ "?" + P_EVENT + "=" + E_LISTEN
+ "&" + P_ID + "=" + id
+ "&" + P_MODE + "=" + aMode;
p(listenURL);
if (aSubject != null) {
listenURL = listenURL + "&" + P_SUBJECT + "=" + aSubject;
}
p("PushletClient listen()");
p("listenURL : " + listenURL);
// Start listener thread (sync call).
startDataEventListener(aListener, listenURL);
}
/**
* Immediate listener: joins/subscribes and listens in one action.
*/
public void joinListen(PushletClientListener aListener, String aMode, String aSubject) throws PushletException {
stopListen();
String listenURL = pushletURL
+ "?" + P_EVENT + "=" + E_JOIN_LISTEN
+ "&" + P_FORMAT + "=" + FORMAT_XML
+ "&" + P_MODE + "=" + aMode
+ "&" + P_SUBJECT + "=" + aSubject;
// Start listener thread (sync call).
startDataEventListener(aListener, listenURL);
}
/**
* Publish an event through server.
*/
public void publish(String aSubject, Map theAttributes) throws PushletException {
throwOnInvalidSession();
Event event = new Event(E_PUBLISH, theAttributes);
event.setField(P_SUBJECT, aSubject);
event.setField(P_ID, id);
Event response = doControl(event);
throwOnNack(response);
}
/**
* Subscribes, returning subscription id.
*/
public String subscribe(String aSubject, String aLabel) throws PushletException {
throwOnInvalidSession();
Event event = new Event(E_SUBSCRIBE);
event.setField(P_ID, id);
event.setField(P_SUBJECT, aSubject);
// Optional label, is returned in data events
if (aLabel != null) {
event.setField(P_SUBSCRIPTION_LABEL, aLabel);
}
// Send request
Event response = doControl(event);
throwOnNack(response);
return response.getField(P_SUBSCRIPTION_ID);
}
/**
* Subscribes, returning subscription id.
*/
public String subscribe(String aSubject) throws PushletException {
return subscribe(aSubject, null);
}
/**
* Unsubscribes with subscription id.
*/
public void unsubscribe(String aSubscriptionId) throws PushletException {
throwOnInvalidSession();
Event event = new Event(E_UNSUBSCRIBE);
event.setField(P_ID, id);
// Optional subscription id
if (aSubscriptionId != null) {
event.setField(P_SUBSCRIPTION_ID, aSubscriptionId);
}
Event response = doControl(event);
throwOnNack(response);
}
/**
* Unsubscribes from all subjects.
*/
public void unsubscribe() throws PushletException {
unsubscribe(null);
}
/**
* Stop the listener.
*/
public void stopListen() throws PushletException {
if (dataEventListener != null) {
unsubscribe();
dataEventListener.stop();
dataEventListener = null;
}
}
public void setDebug(boolean b) {
debug = b;
}
/**
* Starts DataEventListener and waits for its thread to start.
*/
protected void startDataEventListener(PushletClientListener aListener, String aListenURL) {
// Suggestion by Jeff Nowakowski 29.oct.2006
dataEventListener = new DataEventListener(aListener, aListenURL);
synchronized (dataEventListener) {
dataEventListener.start();
try {
// Wait for data event listener (thread) to start
dataEventListener.wait();
} catch (InterruptedException e) {
}
}
}
private void throwOnNack(Event anEvent) throws PushletException {
if (anEvent.getEventType().equals(E_NACK)) {
throw new PushletException("Negative response: reason=" + anEvent.getField(P_REASON));
}
}
private void throwOnInvalidSession() throws PushletException {
if (id == null) {
throw new PushletException("Invalid pushlet session");
}
}
private Reader openURL(String aURL) throws PushletException {
// Open URL connection with server
try {
p("Connecting to " + aURL);
URL url = new URL(aURL);
URLConnection urlConnection = url.openConnection();
// Disable any kind of caching.
urlConnection.setUseCaches(false);
urlConnection.setDefaultUseCaches(false);
// TODO: later version may use POST
// Enable HTTP POST
// urlConnection.setDoOutput(true);
// Do the POST with Event in XML in body
// OutputStream os = urlConnection.getOutputStream();
// os.write(anEvent.toXML().getBytes());
// os.flush();
// os.close();
// Get the stream from the server.
// reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
// Note: somehow the client does not work with some JVMs when using
// BufferedInputStream... So do unbuffered input.
// p("Opening urlConnection inputstream");
return new InputStreamReader(urlConnection.getInputStream());
} catch (Throwable t) {
warn("openURL() could not open " + aURL, t);
throw new PushletException(" could not open " + aURL, t);
}
}
private Event doControl(Event aControlEvent) throws PushletException {
String controlURL = pushletURL + "?" + aControlEvent.toQueryString();
p("doControl to " + controlURL);
// Open URL connection with server
Reader reader = openURL(controlURL);
// Get Pushlet event from stream
Event event = null;
try {
p("Getting event...");
// Get next event from server
event = EventParser.parse(reader);
//p("Event received " + event);
return event;
} catch (Throwable t) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -