-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.java
More file actions
250 lines (221 loc) · 6.72 KB
/
Command.java
File metadata and controls
250 lines (221 loc) · 6.72 KB
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
249
250
/* Copyright (c) 2006 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.gbase.cmdline;
import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseService;
import com.google.gdata.client.Service;
import com.google.gdata.util.AuthenticationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
/**
* One command that is part of CustomerTool.
* This class contains code that is common to all
* commands. It deals, in particular, with the creation
* of the GData service object.
*/
abstract class Command {
/**
* URL of the google authentication server to use for log in.
*/
private static final String DEFAULT_AUTH_HOSTNAME = "www.google.com";
/**
* Username for google base (e-mail address).
*/
protected String username;
/**
* The user's password.
*/
protected String password;
/**
* Base url of the google base server.
*/
protected FeedURLFactory urlFactory = FeedURLFactory.getDefault();
/**
* Base url of the google authentication server.
*/
private String authenticationServer = DEFAULT_AUTH_HOSTNAME;
/**
* Protocol (http or https) to use to connect to the authentication server.
*/
private String authenticationProtocol = "https";
/**
* Developer key used for identification against the Google Base data API
* servers.
*/
private String key;
/**
* Enables dry-run mode for edit operations.
*/
private boolean dryRun;
/**
* Executes the command.
*
* Call this method only after setting the username and password.
*/
public abstract void execute() throws Exception;
/**
* Sets the username, which is required for {@link #execute()} to work.
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Sets the password, which is required for {@link #execute()} to work.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Sets the Url of the google base server to connect to.
*/
public void setGoogleBaseServerUrl(String url) throws MalformedURLException {
this.urlFactory = new FeedURLFactory(url);
}
/**
* Sets the name of the google authentication server to connect to.
*/
public void setAuthenticationServerUrl(String urlString)
throws MalformedURLException {
URL url = new URL(urlString);
this.authenticationProtocol = url.getProtocol();
this.authenticationServer = url.getHost();
if (url.getPort() != -1) {
this.authenticationServer += ":" + url.getPort();
}
}
public void setKey(String key) {
this.key = key;
}
/**
* Makes sure username and password have been set.
*/
public boolean hasAllIdentificationInformation() {
return username != null && password != null;
}
/**
* Creates the service and sets the username and password for
* authentication.
*
* @return the GData service object to use
* @throws com.google.gdata.util.AuthenticationException
* if authentication failed
*/
protected GoogleBaseService createService()
throws AuthenticationException {
GoogleBaseService service =
new GoogleBaseService("Google.-CustomerTool-1.0",
key,
authenticationProtocol,
authenticationServer);
service.setUserCredentials(username, password);
return service;
}
/**
* Gets the URL of the customer feed.
*
* The feed contains only the items uploaded by this
* specific customer. This is also the feed that is
* used to upload customer data.
*
* @return the url to the customer feed
*/
protected URL getCustomerFeedURL() throws MalformedURLException {
return urlFactory.getItemsFeedURL();
}
/**
* Writes the response (XML feed) to standard output.
*/
protected void outputRawResponse(Service.GDataRequest request)
throws IOException {
InputStream responseStream = request.getResponseStream();
try {
copyStreamContent(responseStream, System.out);
} finally {
responseStream.close();
}
System.out.println();
}
/**
* Reads data from in input stream and write it into an
* output stream.
*/
protected void copyStreamContent(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
int l;
while ( (l=in.read(buffer)) > 0 ) {
out.write(buffer, 0, l);
}
}
/**
* Reads data from standard input and use it in the request.
*
* The data must be the XML feed appropriate for the command.
* For update, get or insert it should be one Atom XML entry.
*/
protected void inputRawRequest(Service.GDataRequest request)
throws IOException {
OutputStream outputStream = request.getRequestStream();
try {
copyStreamContent(System.in, outputStream);
} finally {
outputStream.close();
}
}
/**
* Puts the command in dry-run mode, in which nothing will
* really happen on the server.
*
* @param dryRun
*/
public void setDryRun(boolean dryRun) {
this.dryRun = dryRun;
}
/**
* Builds an edit URL from a string, adding the dry-run parameter
* if necessary.
*
* This method is not applicable to query URLs, for which the dry-run
* parameter is not supported.
*
* @param url the original url
* @return the same URL with maybe some parameters
* @throws MalformedURLException
*/
protected URL fixEditUrl(URL url) throws MalformedURLException {
return fixEditUrl(url.toExternalForm());
}
/**
* Builds an edit URL from a string, adding the dry-run parameter
* if necessary.
*
* This method is not applicable to query URLs, for which the dry-run
* parameter is not supported.
*
* @param url the original url, as a string
* @return the same URL with maybe some parameters
* @throws MalformedURLException
*/
protected URL fixEditUrl(String url) throws MalformedURLException {
if (dryRun) {
char separator = url.contains("?") ? '&' : '?';
url = url + separator + "dry-run=true";
}
return new URL(url);
}
}