Subversion Repositories XServices

Rev

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