Subversion Repositories XServices

Rev

Rev 198 | 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
 
199 brianR 15
import lombok.extern.slf4j.Slf4j;
109 brianR 16
import net.brutex.xservices.types.scm.AttributeType;
17
import net.brutex.xservices.types.scm.ItemListType;
18
import net.brutex.xservices.types.scm.ItemType;
19
import net.brutex.xservices.types.scm.ModuleListType;
20
import net.brutex.xservices.types.scm.ModuleType;
21
import net.brutex.xservices.types.scm.ObjectFactory;
22
import net.brutex.xservices.types.scm.RevisionType;
23
import net.brutex.xservices.types.scm.TagListType;
24
import net.brutex.xservices.types.scmfindings.FindingsListType;
25
import net.brutex.xservices.util.BasicCVSListener;
26
import net.brutex.xservices.util.CVSClient;
27
import net.brutex.xservices.util.CVSRoot;
185 brianR 28
 
29
import org.apache.commons.configuration2.ex.ConfigurationException;
30
import org.apache.commons.jcs.JCS;
31
import org.apache.commons.jcs.access.CacheAccess;
32
import org.apache.commons.jcs.access.exception.CacheException;
94 brianR 33
import org.netbeans.lib.cvsclient.Client;
34
import org.netbeans.lib.cvsclient.command.CommandAbortedException;
35
import org.netbeans.lib.cvsclient.command.CommandException;
109 brianR 36
import org.netbeans.lib.cvsclient.command.FileInfoContainer;
37
import org.netbeans.lib.cvsclient.command.PipedFileInformation;
94 brianR 38
import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
39
import org.netbeans.lib.cvsclient.command.checkout.ModuleListInformation;
40
import org.netbeans.lib.cvsclient.command.log.LogInformation;
109 brianR 41
import org.netbeans.lib.cvsclient.command.log.LogInformation.Revision;
94 brianR 42
import org.netbeans.lib.cvsclient.command.log.RlogCommand;
43
import org.netbeans.lib.cvsclient.connection.AuthenticationException;
109 brianR 44
import org.netbeans.lib.cvsclient.event.EventManager;
94 brianR 45
import org.netbeans.lib.cvsclient.event.FileInfoEvent;
46
 
199 brianR 47
@Slf4j
94 brianR 48
public class CVSInfoImpl implements CVSInfo {
199 brianR 49
 
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();
199 brianR 56
		log.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) {
199 brianR 124
						log.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
 
199 brianR 145
			log.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) {
199 brianR 152
			log.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();
199 brianR 175
		log.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
 
199 brianR 203
			log.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) {
199 brianR 208
				log.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";
199 brianR 223
		log.debug("Retrieving Tags from cache using key '" + cachekey
109 brianR 224
				+ "'");
225
		TagListType tags = (TagListType) getCacheInstance().get(cachekey);
226
		if (tags != null) {
199 brianR 227
			log.debug("Delivering Tags from cache.");
109 brianR 228
			return Response.ok(tags).build();
229
		}
199 brianR 230
		log.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) {
199 brianR 242
			log.debug("Retrieving file content from cache using key '"
109 brianR 243
					+ cachekey + "'");
244
			list = (ItemListType) getCacheInstance().get(cachekey);
245
		}
246
 
247
		if (list != null) {
199 brianR 248
			log.debug("Delivering file content from cache.");
109 brianR 249
			return Response.ok(list).build();
250
		}
251
 
199 brianR 252
		log.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
 
199 brianR 309
			log.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 {
199 brianR 333
			log.trace("Getting cache instance named 'CVSCache'");
109 brianR 334
			jcs = JCS.getInstance("CVSCache");
94 brianR 335
		} catch (CacheException e) {
199 brianR 336
			log.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;
199 brianR 350
			log.debug("Fetch searchFileContent response from cache using cachekey '"
109 brianR 351
							+ cachestring + "'");
352
			FindingsListType result = (FindingsListType) getCacheInstance()
353
					.get(cachestring);
354
			if (result != null)
199 brianR 355
				log.debug("Found object for key '" + cachestring
109 brianR 356
						+ "' in cache.");
357
			else {
199 brianR 358
				log.debug("Found no object for key '" + cachestring
109 brianR 359
						+ "' in cache.");
360
			}
361
 
362
			if (result != null)
363
				return Response.ok(result).build();
199 brianR 364
		} catch (CommandAbortedException | AuthenticationException | ConfigurationException e) {
109 brianR 365
			e.printStackTrace();
366
		}
199 brianR 367
        return Response.noContent().build();
109 brianR 368
	}
369
 
370
	private AttributeType getAttribute(String name, String value) {
371
		AttributeType attribute = this.FACTORY.createAttributeType();
372
		attribute.setName(name);
373
		attribute.setValue(value);
374
		return attribute;
375
	}
376
}