Parent: [r19] (diff)

Child: [r24] (diff)

Download this file

showFileDetails.java    297 lines (259 with data), 10.5 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-12T00:00:00Z
* LicenseName: NOASSERTION
* FileName: showFileDetails.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> This class displays the details about a given file that
* has been selected on the treeview or throught some similar manner </text>
*/
package spdx;
import GUI.NodeType;
import GUI.TreeNodeSPDX;
import GUI.swingUtils;
import definitions.Messages;
import definitions.is;
import java.io.File;
import script.Plugin;
import script.log;
import spdxlib.FileInfo;
import main.core;
import main.param;
import spdxlib.SPDXfile;
import spdxlib.tools;
import utils.html;
import www.WebRequest;
/**
*
* @author Nuno Brito, 12th of November 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class showFileDetails extends Plugin{
@Override
public void startup(){
// react whenever a tree node is changed
log.hooks.addAction(Messages.TreeNodeChanged, thisFile, "processFile");
}
/**
* list some information according to a filter
* @param request
*/
public void specific(WebRequest request) {
// we need the "file" parameter to tell us what to detail
String spdxTarget = request.getParameter(param.spdx);
// does this file exists?
File spdxFile = tools.getFile(spdxTarget, request);
if(spdxFile == null){
return;
}
// get what we want to show
String targetFile = request.getParameter(param.file);
// value can't be empty
if(targetFile == null){
request.setAnswer("No filter specified");
return;
}
// start the processing
SPDXfile spdx = new SPDXfile(spdxFile);
// go through all files inside the document
for(FileInfo file : spdx.fileSection.files){
// have we (finally) found a match?
if(targetFile.equals(file.getName())){
String result = showFileDetails(file);
request.setAnswer(result);
return;
}
}
request.setAnswer("Didn't found " + targetFile);
}
/**
* Checks if the node of a file has been selected. If this is the case then
* show as much details as possible about the file
*/
void processFile(){
// ensure we get to know which node is selected
TreeNodeSPDX node = swingUtils.getSelectedNode();
// no need to continue if there is nothing selected
if(node == null){
return;
}
// process files
if(node.nodeType != NodeType.file){
return;
}
// we're talking about tree nodes, get the respective information
FileInfo file = (FileInfo) node.getUserObject();
// create the summary for the requested file
String output = showFileDetails(file);
// this only applies to calls from the treeview
core.studio.editorPane(is.contentHTML, Boolean.FALSE, 0, output);
}
/**
* Tries to discover the file extension based on the SPDX file name
* @return
*/
String getFileExtension(String filename){
// preflight checks
if(filename.contains(".")==false){
return "";
}
if(filename.contains("/")){
return "";
}
int pos = filename.lastIndexOf(".");
String extension = "filetype:" + filename.substring(pos+1);
return extension;
}
/**
* Do the actual part of showing the details for this file
* @param node
*/
private String showFileDetails(FileInfo file) {
// where we store the end result
// FileInfo file = (FileInfo) node.getUserObject();
String googleTerm = "";
String filename = file.getName();
// get the file extension if available
//String extension = getFileExtension(filename);
try{
// get the package name to narrow down the results
// TreeNodeSPDX parent = (TreeNodeSPDX) node.getParent();
// TreeNodeSPDX root = (TreeNodeSPDX) parent.getParent();
// we need to remove misleading terms from the filename
String filteredName = file.getName();
// root.id.toLowerCase();
filteredName = filteredName.replace(".tar.gz", "");
filteredName = filteredName.replace(".zip", "");
filteredName = filteredName.replace(".jar", "");
filteredName = filteredName.replace(".7z", "");
filteredName = filteredName.replace(".tar", "");
filteredName = filteredName.replace(".bz2", "");
// build up our google query
googleTerm = filteredName
+ " %22"
+ filename
+ "%22 "
//+ extension
;
} catch (Exception e){
log.write(is.ERROR, "SFD01 - Error occurred when trying to build"
+ " a search keyword for google");
}
// do the introduction of this file with a breadcrumb
// String[] fields = node.getUID().split(">>");
// String breadCrumb =
// fields[4]
// + ">"
// + fields[3]
// + ">"
// + fields[1]
// ;
// add a short summary about the file
String summary = "";
if(file.tagFileSize != null){
summary += "This file is sized in " + file.tagFileSize.toString();
}
if(file.tagFileLOC != null){
summary += " with " + file.tagFileLOC + " lines of code"
;
}
if(file.hasLicense()){
summary +=
html.br
+ html.br
+ "Applicable license(s): " + file.getLicense();
}
// do the date creation
//TODO We need to archive file date information
//summary += "Created in " + file.;
String resultIntroduction = ""
+ "<h2>"
+ filename
// + breadCrumb
+ "</h2>"
+ html.div()
+ summary
+ html._div
// + html.br
;
// Add links to find more info on the Internet about the file name
String resultFilename =
html.div()
+ html.linkToSearchYandex("\"" + filename + "\"")
+ html.divider
+ html.linkToSearchGoogle(googleTerm)
// + html.divider
// + html.linkToSearchOhloh(file.tagFileName.toString())
+ html.divider
+ html.linkToSearchGitHub(file.tagFileName.toString())
+ html._div;
String resultSHA1 = "";
if(file.tagFileChecksumSHA1!= null){
resultSHA1 = file.tagFileChecksumSHA1.toString()
+ html.br
+ html.div()
+ html.linkSearch("Find duplicates", "SHA1: "
+ file.tagFileChecksumSHA1.toString())
+ html.divider
+ html.linkToSearchGoogle(
"%22"+ file.tagFileChecksumSHA1.toString() + "%22" )
+ html._div;
}
String resultSHA256 = "";
if(file.tagFileChecksumSHA256!= null){
resultSHA256 = file.tagFileChecksumSHA256.toString()
+ html.div()
// only possible when we have SHA256 hashes available
+ html.linkToSearchMetaScan(file.tagFileChecksumSHA256.toString())
+ " (scan file with +20 antivirus engines)"
+ html._div;
}
String resultMD5 = "";
if(file.tagFileChecksumMD5!= null){
resultMD5 = file.tagFileChecksumMD5.toString()
+ html.div()
// We might be lucky and find an MD5 with info
+ html.linkToSearchGoogle(
"%22"+ file.tagFileChecksumMD5.toString() + "%22" )
+ html._div;
}
String resultSSDEEP = "";
if(file.tagFileChecksumSSDEEP!= null){
String text = file.tagFileChecksumSSDEEP.raw;
// remove the tag header
text = text.replace("FileChecksum: SSDEEP: ", "");
resultSSDEEP = text
+ html.div()
// only possible when we have SHA256 hashes available
+ html.linkSearch("Find similar files", "SSDEEP: "
+ text)
+ html._div;
}
String result = html.div()
+ resultIntroduction
+ html.br
+ html.br
+ swingUtils.addIfNotEmpty("SHA1", resultSHA1)
+ swingUtils.addIfNotEmpty("SHA256", resultSHA256)
+ swingUtils.addIfNotEmpty("MD5",resultMD5)
//+ html.br
+ swingUtils.addIfNotEmpty("SSDEEP", resultSSDEEP)
+ html.br
+ swingUtils.addIfNotEmpty("Look for \""
+filename
+"\""
, resultFilename)
+ html._div
// + swingUtils.addSSDEEP("SSDEEP", file)
// + swingUtils.addIfNotEmpty("Copyright text",file.tagFileCopyrightText.toString())
;
/** missing to add:
* - File path
* - Applicable licenses (when available)
*/
return result;
}
}