/
GalleryComm.java
248 lines (212 loc) · 8.4 KB
/
GalleryComm.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Gallery Remote - a File Upload Utility for Gallery
*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2001 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.gallery.GalleryRemote;
import java.io.IOException;
import java.net.URL;
import java.net.UnknownHostException;
import java.awt.*;
import HTTPClient.*;
import com.gallery.GalleryRemote.model.Album;
import com.gallery.GalleryRemote.model.Gallery;
import com.gallery.GalleryRemote.prefs.GalleryProperties;
import com.gallery.GalleryRemote.prefs.PreferenceNames;
import com.gallery.GalleryRemote.util.GRI18n;
import javax.swing.*;
/**
* This interface is a temporary mechanism to let us use version
* 1 and 2 of the protocol by changing a little code -- a replacement for
* this is under development that will allow a GalleryRemote client
* to automatically determine what protocol it should use given
* a Gallery and to use the appropriate implementation.
*
* @author <a href="mailto:tim_miller@users.sourceforge.net">Tim Miller</a>
*/
public abstract class GalleryComm implements PreferenceNames {
private static final String MODULE = "GalComm";
int[] capabilities = null;
private static int lastRespCode = 0;
/**
* Flag to hold logged in status. Only need to log in once.
*/
protected boolean isLoggedIn = false;
/* -------------------------------------------------------------------------
* STATIC INITIALIZATON
*/
static {
/* Enable customized AuthorizePopup */
AuthorizePopup.enable();
/* Configures HTTPClient to accept all cookies
* this should be done at least once per GalleryRemote
* invokation */
CookieModule.setCookiePolicyHandler(new CookiePolicyHandler() {
public boolean acceptCookie(Cookie cookie, RoRequest req, RoResponse resp) {
Log.log(Log.LEVEL_TRACE, MODULE, "Accepting cookie: " + cookie);
return true;
}
public boolean sendCookie(Cookie cookie, RoRequest req) {
Log.log(Log.LEVEL_TRACE, MODULE, "Sending cookie: " + cookie);
return true;
}
});
}
/**
* Causes the GalleryComm instance to upload the pictures in the
* associated Gallery to the server.
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void uploadFiles( StatusUpdate su, boolean async ) {
throw new RuntimeException( "This method is not available on this protocol" );
}
/**
* Causes the GalleryComm instance to fetch the albums contained by
* associated Gallery from the server.
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void fetchAlbums( StatusUpdate su, boolean async ) {
throw new RuntimeException( "This method is not available on this protocol" );
}
/**
* Causes the GalleryComm instance to fetch the album properties
* for the given Album.
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void albumInfo( StatusUpdate su, Album a, boolean async ) {
throw new RuntimeException( "This method is not available on this protocol" );
}
/**
* Causes the GalleryComm instance to create a new album as a child of
* the specified album (or at the root if album is null)
*
* @param su an instance that implements the StatusUpdate interface.
* @param parentAlbum if null, create the album in the root of the gallery; otherwise
* create as a child of the given album
*/
public String newAlbum( StatusUpdate su, Album parentAlbum,
String newAlbumName, String newAlbumTitle,
String newAlbumDesc, boolean async ) {
throw new RuntimeException( "This method is not available on this protocol" );
}
public void fetchAlbumImages(StatusUpdate su, Album a, boolean async) {
throw new RuntimeException( "This method is not available on this protocol" );
}
public void logOut() {
isLoggedIn = false;
CookieModule.discardAllCookies();
}
public boolean isLoggedIn() {
return isLoggedIn;
}
public boolean hasCapability(int capability) {
return java.util.Arrays.binarySearch(capabilities, capability) >= 0;
}
/** Return true if the last communication attempt failed with authorization error */
public static boolean wasAuthFailure () {
boolean result = (lastRespCode == 401);
return result;
}
public static GalleryComm getCommInstance(StatusUpdate su, URL url, Gallery g) {
try {
GalleryProperties p = GalleryRemote.getInstance().properties;
// set proxy info
if (p.getBooleanProperty(USE_PROXY)) {
String hostname = p.getProperty(PROXY_HOST);
int port = 80;
try { port = p.getIntProperty(PROXY_PORT); } catch (NumberFormatException e) {}
String username = p.getProperty(PROXY_USERNAME);
Log.log(Log.LEVEL_TRACE, MODULE, "Setting proxy to " + hostname + ":" + port);
HTTPConnection.setProxyServer(hostname, port);
if (username != null && username.length() > 0) {
String password = p.getBase64Property(PROXY_PASSWORD);
Log.log(Log.LEVEL_TRACE, MODULE, "Setting proxy auth to " + username + ":" + password);
AuthorizationInfo.addBasicAuthorization(hostname, port, "",
username, password);
}
} else {
HTTPConnection.setProxyServer(null, 0);
}
// create a connection
HTTPConnection mConnection = new HTTPConnection( url );
if (g.getType() == Gallery.TYPE_STANDALONE) {
// assemble the URL
String urlPath = url.getFile();
Log.log(Log.LEVEL_TRACE, MODULE, "Trying protocol 2 for " + url);
// Test GalleryComm2
String urlPath2 = urlPath + ( (urlPath.endsWith( "/" )) ? GalleryComm2.SCRIPT_NAME : "/" + GalleryComm2.SCRIPT_NAME );
if (tryComm(su, mConnection, urlPath2)) {
Log.log(Log.LEVEL_TRACE, MODULE, "Server has protocol 2");
return new GalleryComm2(g);
}
Log.log(Log.LEVEL_TRACE, MODULE, "Trying protocol 1 for " + url);
// Test GalleryComm1
// BUT: only if first try was not status code 401 = authorization failure
String urlPath1 = urlPath + ( (urlPath.endsWith( "/" )) ? GalleryComm1.SCRIPT_NAME : "/" + GalleryComm1.SCRIPT_NAME );
if (lastRespCode != 401 && tryComm(su, mConnection, urlPath1)) {
Log.log(Log.LEVEL_TRACE, MODULE, "Server has protocol 1");
return new GalleryComm1(g);
}
} else {
// if Gallery is embedded, only support protocol 2
return new GalleryComm2(g);
}
} catch (HTTPClient.ProtocolNotSuppException e) {
Log.logException(Log.LEVEL_ERROR, MODULE, e);
}
return null;
}
private static boolean tryComm(StatusUpdate su, HTTPConnection mConnection, String urlPath) {
try {
HTTPResponse rsp = null;
rsp = mConnection.Head(urlPath);
// handle 30x redirects
// (and authorization failure)
int rspCode = rsp.getStatusCode(); // try actual communcation
lastRespCode = rspCode;
if (rspCode >= 300 && rspCode < 400) {
// retry, the library will have fixed the URL
rsp = mConnection.Post(urlPath);
rspCode = rsp.getStatusCode();
}
Log.log(Log.LEVEL_TRACE, MODULE, "tryComm " + urlPath + ": " + rspCode);
return rspCode == 200;
} catch (UnknownHostException uhe) {
su.error("Unknown host: " + mConnection.getHost());
} catch (IOException ioe) {
// we can't directly catch the SSLPeerUnverifiedException, because Java 1.3 barfs and prevents
// loading this class at all. Instead, cast it inside another try-catch...
try {
if (ioe instanceof javax.net.ssl.SSLPeerUnverifiedException) {
Log.logException(Log.LEVEL_ERROR, MODULE, ioe);
JOptionPane.showMessageDialog((Component) su, GRI18n.getString(MODULE, "noAuth"), GRI18n.getString(MODULE, "error"), JOptionPane.ERROR_MESSAGE);
} else {
Log.logException(Log.LEVEL_ERROR, MODULE, ioe);
}
} catch (NoClassDefFoundError ncdfe) {
Log.logException(Log.LEVEL_ERROR, MODULE, ioe);
}
} catch (ModuleException me) {
Log.logException(Log.LEVEL_ERROR, MODULE, me);
}
return false;
}
}