Subversion Repositories XServices

Rev

Rev 94 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 brianR 1
package net.brutex.xservices.ws.rs;
2
 
109 brianR 3
import java.io.ByteArrayOutputStream;
94 brianR 4
import java.io.File;
109 brianR 5
import java.io.FileReader;
6
import java.io.IOException;
7
import java.io.PrintStream;
8
import java.net.URI;
94 brianR 9
import java.util.List;
10
import java.util.StringTokenizer;
11
import javax.ws.rs.core.HttpHeaders;
12
import javax.ws.rs.core.Response;
109 brianR 13
import javax.ws.rs.core.Response.ResponseBuilder;
14
import net.brutex.xservices.types.scm.AttributeType;
15
import net.brutex.xservices.types.scm.ItemListType;
16
import net.brutex.xservices.types.scm.ItemType;
17
import net.brutex.xservices.types.scm.ModuleListType;
18
import net.brutex.xservices.types.scm.ModuleType;
19
import net.brutex.xservices.types.scm.ObjectFactory;
20
import net.brutex.xservices.types.scm.RevisionType;
21
import net.brutex.xservices.types.scm.TagListType;
22
import net.brutex.xservices.types.scmfindings.FindingsListType;
23
import net.brutex.xservices.util.BasicCVSListener;
24
import net.brutex.xservices.util.CVSClient;
25
import net.brutex.xservices.util.CVSRoot;
94 brianR 26
import org.apache.commons.configuration.ConfigurationException;
27
import org.apache.jcs.JCS;
28
import org.apache.jcs.access.exception.CacheException;
29
import org.apache.log4j.Logger;
30
import org.netbeans.lib.cvsclient.Client;
31
import org.netbeans.lib.cvsclient.command.CommandAbortedException;
32
import org.netbeans.lib.cvsclient.command.CommandException;
109 brianR 33
import org.netbeans.lib.cvsclient.command.FileInfoContainer;
34
import org.netbeans.lib.cvsclient.command.PipedFileInformation;
94 brianR 35
import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
36
import org.netbeans.lib.cvsclient.command.checkout.ModuleListInformation;
37
import org.netbeans.lib.cvsclient.command.log.LogInformation;
109 brianR 38
import org.netbeans.lib.cvsclient.command.log.LogInformation.Revision;
94 brianR 39
import org.netbeans.lib.cvsclient.command.log.RlogCommand;
40
import org.netbeans.lib.cvsclient.connection.AuthenticationException;
109 brianR 41
import org.netbeans.lib.cvsclient.event.EventManager;
94 brianR 42
import org.netbeans.lib.cvsclient.event.FileInfoEvent;
43
 
44
public class CVSInfoImpl implements CVSInfo {
45
	final Logger logger = Logger.getLogger(CVSInfoImpl.class);
109 brianR 46
	final ObjectFactory FACTORY = new ObjectFactory();
47
	final ItemListType list = this.FACTORY.createItemListType();
94 brianR 48
 
49
	public Response getRepositoryFiles(HttpHeaders h, File f, String modules,
109 brianR 50
			boolean isRecursive, boolean showRevisions, boolean forceNoCache) {
94 brianR 51
		String cachekey = "getFiles" + f.toURI().toString();
109 brianR 52
		this.logger.debug("forceNoCache=" + forceNoCache);
53
		ItemListType cacheresult = (ItemListType) getCacheInstance().get(
94 brianR 54
				cachekey);
55
 
109 brianR 56
		if ((!forceNoCache) && (cacheresult != null)) {
57
			return Response.ok(cacheresult).build();
58
		}
59
		Client client;
60
		try {
61
			final CVSClient cvsclient = new CVSClient(f);
62
			client = cvsclient.client;
94 brianR 63
 
109 brianR 64
			client.getEventManager().addCVSListener(new BasicCVSListener() {
65
				public void fileInfoGenerated(FileInfoEvent arg0) {
66
					LogInformation info = (LogInformation) arg0
67
							.getInfoContainer();
68
					String repoPath = cvsclient.client.getRepository();
69
 
70
					ItemType cvsfile = CVSInfoImpl.this.FACTORY
71
							.createItemType();
72
					cvsfile.setIsLeaf(true);
73
					cvsfile.setIsBinary(false);
74
 
75
					cvsfile.setFullname(info.getRepositoryFilename().substring(
76
							repoPath.length() + 2,
77
							info.getRepositoryFilename().length() - 2));
78
 
79
					cvsfile.setRemotename(info.getRepositoryFilename());
80
					cvsfile.setRemotefullname(info.getRepositoryFilename());
81
					RevisionType revision = CVSInfoImpl.this.FACTORY
82
							.createRevisionType();
83
					revision.setRevision(info.getHeadRevision());
84
					revision.setComment(info.getDescription());
85
					cvsfile.setTipRevision(revision);
86
 
87
					for (LogInformation.Revision r : info.getRevisionList()) {
88
						revision = CVSInfoImpl.this.FACTORY
89
								.createRevisionType();
90
						revision.setRevision(r.getNumber());
91
						revision.setComment(r.getMessage());
92
						cvsfile.getRevisions().add(revision);
94 brianR 93
					}
94
 
109 brianR 95
					cvsfile.getAttributes().add(
96
							CVSInfoImpl.this.getAttribute("TOTALREVISIONS",
97
									info.getTotalRevisions()));
98
					cvsfile.getAttributes().add(
99
							CVSInfoImpl.this.getAttribute("BRANCH",
100
									info.getBranch()));
101
					cvsfile.getAttributes().add(
102
							CVSInfoImpl.this.getAttribute(
103
									"KEYWORDSUBSTITUTION",
104
									info.getKeywordSubstitution()));
105
					cvsfile.getAttributes().add(
106
							CVSInfoImpl.this.getAttribute("LOCKS",
107
									info.getLocks()));
108
					cvsfile.getAttributes().add(
109
							CVSInfoImpl.this.getAttribute("SELECTEDREVISIONS",
110
									info.getSelectedRevisions()));
111
					cvsfile.setROOT(cvsclient.getRoot().host + "@"
112
							+ cvsclient.getRoot().repository);
113
 
114
					CVSInfoImpl.this.list.getItems().add(cvsfile);
115
 
116
					String key = CVSClient.generateID(cvsfile);
117
					try {
118
						CVSInfoImpl.this.getCacheInstance().put(key, cvsfile);
119
					} catch (CacheException e) {
120
						CVSInfoImpl.this.logger.error("Could not cache item '"
121
								+ key + "'", e);
122
					}
94 brianR 123
				}
109 brianR 124
			});
125
			RlogCommand rlog = new RlogCommand();
126
			StringTokenizer tk = new StringTokenizer(modules, ",");
127
			while (tk.hasMoreTokens()) {
128
				rlog.setModule(tk.nextToken());
94 brianR 129
			}
109 brianR 130
			if (rlog.getModules().length == 0) {
131
				rlog.setModule("");
132
			}
133
			rlog.setDefaultBranch(false);
94 brianR 134
 
109 brianR 135
			rlog.setNoTags(false);
136
 
137
			rlog.setHeaderAndDescOnly(false);
138
 
139
			rlog.setRecursive(isRecursive);
140
 
141
			this.logger.info("Executing CVS command '" + rlog.getCVSCommand()
142
					+ "' against '" + cvsclient.getRoot().host + "@"
143
					+ cvsclient.getRoot().repository + "'");
144
			client.executeCommand(rlog, cvsclient.getGlobalOptions());
145
 
146
			getCacheInstance().put(cachekey, this.list);
147
		} catch (ConfigurationException e) {
148
			this.logger.error("CVS Configuration File '" + f.getAbsolutePath()
149
					+ f.getName() + "'not found.", e);
150
		} catch (CommandAbortedException e) {
151
			e.printStackTrace();
152
		} catch (AuthenticationException e) {
153
			e.printStackTrace();
154
		} catch (CommandException e) {
155
			e.printStackTrace();
156
		} catch (CacheException e) {
157
			e.printStackTrace();
94 brianR 158
		}
109 brianR 159
 
94 brianR 160
		if (!showRevisions) {
109 brianR 161
			for (ItemType t : this.list.getItems()) {
162
				t.getRevisions().clear();
94 brianR 163
			}
164
		}
109 brianR 165
 
166
		return Response.ok(this.list).build();
94 brianR 167
	}
168
 
169
	public Response getModules(HttpHeaders h, File f, boolean forceNoCache) {
109 brianR 170
		String cachekey = "Modules" + f.toURI().toString();
171
		this.logger.debug("forceNoCache=" + forceNoCache);
94 brianR 172
 
109 brianR 173
		ModuleListType response = (ModuleListType) getCacheInstance().get(
94 brianR 174
				cachekey);
109 brianR 175
		if ((!forceNoCache) && (response != null)) {
176
			return Response.ok(response).build();
94 brianR 177
		}
178
		try {
179
			CVSClient cvsclient = new CVSClient(f);
180
			Client client = cvsclient.client;
109 brianR 181
			final ModuleListType list = this.FACTORY.createModuleListType();
94 brianR 182
 
183
			client.getEventManager().addCVSListener(new BasicCVSListener() {
184
				public void fileInfoGenerated(FileInfoEvent e) {
185
					ModuleListInformation info = (ModuleListInformation) e
186
							.getInfoContainer();
109 brianR 187
					ModuleType module = CVSInfoImpl.this.FACTORY
188
							.createModuleType();
189
					module.setName(info.getModuleName());
190
					module.setStatus(info.getModuleStatus());
191
					module.setPath(info.getPaths());
192
					module.setType(info.getType());
193
					list.getModules().add(module);
94 brianR 194
				}
195
			});
196
			CheckoutCommand co = new CheckoutCommand();
197
			co.setShowModulesWithStatus(true);
198
 
109 brianR 199
			this.logger.info("Executing CVS command '" + co.getCVSCommand()
200
					+ "' against '" + cvsclient.getRoot().host + "@"
201
					+ cvsclient.getRoot().repository + "'");
94 brianR 202
			client.executeCommand(co, cvsclient.getGlobalOptions());
109 brianR 203
			if (list.getModules().size() == 0) {
204
				this.logger.warn("Repository '"
205
						+ cvsclient.getRoot().repository
206
						+ "' does not have modules");
94 brianR 207
			}
109 brianR 208
 
94 brianR 209
			getCacheInstance().put(cachekey, list);
109 brianR 210
			return Response.ok(list).build();
94 brianR 211
		} catch (Exception e) {
212
			e.printStackTrace();
213
		}
214
		return Response.serverError().build();
215
	}
216
 
109 brianR 217
	public Response getTags(HttpHeaders h, File f, boolean withFiles) {
218
		String cachekey = f.toURI().toString() + ":taglist";
219
		this.logger.debug("Retrieving Tags from cache using key '" + cachekey
220
				+ "'");
221
		TagListType tags = (TagListType) getCacheInstance().get(cachekey);
222
		if (tags != null) {
223
			this.logger.debug("Delivering Tags from cache.");
224
			return Response.ok(tags).build();
225
		}
226
		this.logger.warn("Taglist not found in cache.");
227
		return Response.noContent().build();
228
	}
229
 
230
	public Response getFileContent(HttpHeaders h, File f, String filestring,
231
			boolean forceNoCache) {
232
		final ItemType result = this.FACTORY.createItemType();
233
		final String cachekey = f.toURI().toString() + ":" + filestring
234
				+ ":content";
235
		ItemListType list = null;
236
 
237
		if (!forceNoCache) {
238
			this.logger.debug("Retrieving file content from cache using key '"
239
					+ cachekey + "'");
240
			list = (ItemListType) getCacheInstance().get(cachekey);
241
		}
242
 
243
		if (list != null) {
244
			this.logger.debug("Delivering file content from cache.");
245
			return Response.ok(list).build();
246
		}
247
 
248
		this.logger.warn("File content not found in cache.");
249
		list = this.FACTORY.createItemListType();
250
		try {
251
			CVSClient cvsclient = new CVSClient(f);
252
			Client client = cvsclient.getClient();
253
 
254
			CheckoutCommand checkout = new CheckoutCommand();
255
			BasicCVSListener listener = new BasicCVSListener() {
256
				public void fileInfoGenerated(FileInfoEvent arg0) {
257
					System.out.println(arg0.getInfoContainer().getFile()
258
							.toURI().toString());
259
					PipedFileInformation info = (PipedFileInformation) arg0
260
							.getInfoContainer();
261
					result.setName(info.getFile().getName());
262
					try {
263
						boolean isBinary = false;
264
						result.setIsBinary(isBinary);
265
						result.setRemotename(info.getRepositoryFileName());
266
						RevisionType revision = CVSInfoImpl.this.FACTORY
267
								.createRevisionType();
268
						revision.setRevision(info.getRepositoryRevision());
269
						revision.setComment("");
270
 
271
						if (!isBinary) {
272
							FileReader fin = new FileReader(info.getTempFile());
273
 
274
							ByteArrayOutputStream bout = new ByteArrayOutputStream();
275
							StringBuffer sbuf = new StringBuffer();
276
							int c;
277
							while ((c = fin.read()) != -1) {
278
								bout.write(c);
279
								sbuf.append((char) c);
280
							}
281
							result.setData(bout.toByteArray());
282
							result.setContent(sbuf.toString());
283
						}
284
 
285
					} catch (IOException e2) {
286
						e2.printStackTrace();
287
					} catch (NullPointerException ne) {
288
						ne.printStackTrace();
289
					}
290
 
291
					String key = CVSClient.generateID(result);
292
					try {
293
						CVSInfoImpl.this.getCacheInstance().put(cachekey,
294
								result);
295
					} catch (CacheException e1) {
296
						e1.printStackTrace();
297
					}
298
				}
299
			};
300
			client.getEventManager().addCVSListener(listener);
301
 
302
			checkout.setModule(filestring);
303
			checkout.setPipeToOutput(true);
304
 
305
			this.logger.info("Execute CVS command '" + checkout.getCVSCommand()
306
					+ "' against '" + cvsclient.getRoot().host + "@"
307
					+ cvsclient.getRoot().repository + "'");
308
			client.executeCommand(checkout, cvsclient.getGlobalOptions());
309
		} catch (CommandAbortedException e) {
310
			e.printStackTrace();
311
		} catch (ConfigurationException e) {
312
			e.printStackTrace();
313
		} catch (AuthenticationException e) {
314
			e.printStackTrace();
315
		} catch (CommandException e) {
316
			e.printStackTrace();
317
		}
318
 
319
		if (result.getContent() != null) {
320
			return Response.ok(result).build();
321
		}
322
		return Response.noContent().build();
323
	}
324
 
94 brianR 325
	public JCS getCacheInstance() {
326
		JCS jcs = null;
109 brianR 327
		String cacheinstance = "CVSCache";
94 brianR 328
		try {
109 brianR 329
			this.logger.trace("Getting cache instance named 'CVSCache'");
330
			jcs = JCS.getInstance("CVSCache");
94 brianR 331
		} catch (CacheException e) {
109 brianR 332
			this.logger.error("Failed to get cache instance", e);
94 brianR 333
			e.printStackTrace();
334
		}
335
		return jcs;
336
	}
337
 
109 brianR 338
	public Response searchFileContent(HttpHeaders h, File f,
339
			String file_regexp, String content_regexp, boolean forceNoCache) {
340
		try {
341
			CVSClient client = new CVSClient(f);
342
			String cvsroot = client.getRoot().host + "@"
343
					+ client.getRoot().repository;
344
 
345
			String cachestring = "FINDINGS-" + cvsroot;
346
			this.logger
347
					.debug("Fetch searchFileContent response from cache using cachekey '"
348
							+ cachestring + "'");
349
			FindingsListType result = (FindingsListType) getCacheInstance()
350
					.get(cachestring);
351
			if (result != null)
352
				this.logger.debug("Found object for key '" + cachestring
353
						+ "' in cache.");
354
			else {
355
				this.logger.debug("Found no object for key '" + cachestring
356
						+ "' in cache.");
357
			}
358
 
359
			if (result != null)
360
				return Response.ok(result).build();
361
		} catch (CommandAbortedException e) {
362
			e.printStackTrace();
363
		} catch (ConfigurationException e) {
364
			e.printStackTrace();
365
		} catch (AuthenticationException e) {
366
			e.printStackTrace();
367
		}
368
		return Response.noContent().build();
369
	}
370
 
371
	private AttributeType getAttribute(String name, String value) {
372
		AttributeType attribute = this.FACTORY.createAttributeType();
373
		attribute.setName(name);
374
		attribute.setValue(value);
375
		return attribute;
376
	}
377
}