1   package com.trendmicro.grid.acl.l0;
2   
3   import com.trendmicro.grid.acl.l0.datatypes.FileDetails;
4   import com.trendmicro.grid.acl.l0.datatypes.FileIdentifier;
5   import com.trendmicro.grid.acl.l0.datatypes.FileInformation;
6   import com.trendmicro.grid.acl.l0.datatypes.IsTrueResult;
7   import net.sf.tinyjee.util.Hex;
8   import org.springframework.stereotype.Service;
9   
10  import javax.annotation.PostConstruct;
11  import javax.annotation.Resource;
12  import javax.ws.rs.*;
13  import java.util.List;
14  import java.util.Set;
15  
16  /**
17   * Implements a REST styled interface on top of the SOAP api using JAX-RS
18   *
19   * @author Juergen_Kellerer, 2010-04-30
20   * @version 1.0
21   */
22  @Service
23  //@PublicRequestContext - Not required as injected instance is already wrapped by an AspectJ proxy.
24  @Path("/files")
25  @Produces({"application/xml", "application/json"})
26  public class PublicFileRestService implements Level0RestService {
27  
28  	@Resource
29  	Set<PublicFileService> services;
30  	PublicFileService fileService;
31  
32  	@PostConstruct
33  	void init() {
34  		// Selecting the public implementation only.
35  		for (PublicFileService service : services) {
36  			if (!(service instanceof FileService)) fileService = service;
37  		}
38  	}
39  
40  	private static String resultToString(IsTrueResult result) {
41  		if (result != null) {
42  			switch (result) {
43  				case Yes:
44  					return "true";
45  				case No:
46  					return "false";
47  			}
48  		}
49  		return null;
50  	}
51  
52  	private static FileInformation convertJpaFileInformation(FileInformation fi) {
53  		if (fi != null) {
54  			String[] a = new String[fi.getTags().size()];
55  			return new FileInformation(fi.getFirstSeen(), fi.getLastRetrieved(), fi.getLastProcessed(),
56  					fi.getTags().toArray(a), fi.getSourcePackageCount() == null ? 0 : fi.getSourcePackageCount(),
57  					fi.getSourceSiteCount() == null ? 0 : fi.getSourceSiteCount());
58  		} else {
59  			return null;
60  		}
61  	}
62  
63  	private static FileDetails convertJpaFileDetails(FileDetails fd) {
64  		if (fd != null)
65  			return new FileDetails(fd.getIdentifier(), fd.getInformation(), fd.getMetadata());
66  		else
67  			return null;
68  	}
69  
70  	/**
71  	 * Allows to query the GRID whether a file is known to be good.
72  	 *
73  	 * @param sha1OrMd5Hash The SHA1 or MD5 hash (as hexadecimal string) of the file to query.
74  	 * @return 'true' if the file is good, 'false' if not, 'null' (HTTP 204 No Content) if it's unknown.
75  	 * @throws AuthenticationException In case of this service requires authentication and the current user session
76  	 *                                 is not authenticated or doesn't have the right to access the service.
77  	 * @RequiredRoles ROLE_RUN_HASH_QUERIES
78  	 */
79  	@GET
80  	@Produces("text/plain")
81  	@Path("/isKnownGood/{sha1OrMd5}")
82  	public String isFileKnownGood(
83  			@PathParam("sha1OrMd5") String sha1OrMd5Hash) throws AuthenticationException {
84  		IsTrueResult result = fileService.isFileKnownGood(new FileIdentifier(Hex.decode(sha1OrMd5Hash)));
85  		return resultToString(result);
86  	}
87  
88  	/**
89  	 * Allows to query the GRID whether a file is known to be good.
90  	 *
91  	 * @param sha1Hash The SHA1 hash (as hexadecimal string) of the file to query.
92  	 * @param md5Hash  The MD5 hash (as hexadecimal string) of the file to query.
93  	 * @return 'true' if the file is good, 'false' if not, 'null' (HTTP 204 No Content) if it's unknown.
94  	 * @throws AuthenticationException In case of this service requires authentication and the current user session
95  	 *                                 is not authenticated or doesn't have the right to access the service.
96  	 * @RequiredRoles ROLE_RUN_HASH_QUERIES
97  	 */
98  	@GET
99  	@Produces("text/plain")
100 	@Path("/isKnownGood/{sha1}-{md5}")
101 	public String isFileKnownGood(
102 			@PathParam("sha1") String sha1Hash, @PathParam("md5") String md5Hash) throws AuthenticationException {
103 		FileIdentifier identifier = new FileIdentifier(sha1Hash, md5Hash);
104 		IsTrueResult result = fileService.isFileKnownGood(identifier);
105 		return resultToString(result);
106 	}
107 
108 	/**
109 	 * Allows to query the GRID whether a file is known to be good and not included in the high risk category.
110 	 *
111 	 * @param sha1OrMd5Hash The SHA1 or MD5 hash (as hexadecimal string) of the file to query.
112 	 * @return 'true' if the file is good, 'false' if not, 'null' (HTTP 204 No Content) if it's unknown.
113 	 * @throws AuthenticationException In case of this service requires authentication and the current user session
114 	 *                                 is not authenticated or doesn't have the right to access the service.
115 	 * @RequiredRoles ROLE_RUN_HASH_QUERIES
116 	 */
117 	@GET
118 	@Produces("text/plain")
119 	@Path("/isPureWhite/{sha1OrMd5}")
120 	public String isFilePureWhite(
121 			@PathParam("sha1OrMd5") String sha1OrMd5Hash) throws AuthenticationException {
122 		IsTrueResult result = fileService.isFilePureWhite(new FileIdentifier(Hex.decode(sha1OrMd5Hash)));
123 		return resultToString(result);
124 	}
125 
126 	/**
127 	 * Allows to query the GRID whether a file is known to be good and not included in the high risk category.
128 	 *
129 	 * @param sha1Hash The SHA1 hash (as hexadecimal string) of the file to query.
130 	 * @param md5Hash  The MD5 hash (as hexadecimal string) of the file to query.
131 	 * @return 'true' if the file is good, 'false' if not, 'null' (HTTP 204 No Content) if it's unknown.
132 	 * @throws AuthenticationException In case of this service requires authentication and the current user session
133 	 *                                 is not authenticated or doesn't have the right to access the service.
134 	 * @RequiredRoles ROLE_RUN_HASH_QUERIES
135 	 */
136 	@GET
137 	@Produces("text/plain")
138 	@Path("/isPureWhite/{sha1}-{md5}")
139 	public String isFilePureWhite(
140 			@PathParam("sha1") String sha1Hash, @PathParam("md5") String md5Hash) throws AuthenticationException {
141 		IsTrueResult result = fileService.isFilePureWhite(new FileIdentifier(sha1Hash, md5Hash));
142 		return resultToString(result);
143 	}
144 
145 	/**
146 	 * Allows to query the GRID whether a file is tagged with a set of tags.
147 	 *
148 	 * @param sha1OrMd5Hash The SHA1 or MD5 hash (as hexadecimal string) of the file to query.
149 	 * @return 'true' if the file is tagged with all given tags, 'false' if not, 'null' (HTTP 204 No Content) if it's unkown.
150 	 * @throws AuthenticationException In case of this service requires authentication and the current user session
151 	 *                                 is not authenticated or doesn't have the right to access the service.
152 	 * @RequiredRoles ROLE_RUN_HASH_QUERIES
153 	 */
154 	@GET
155 	@Produces("text/plain")
156 	@Path("/isTaggedWith/{sha1OrMd5}")
157 	public String isFileTaggedWith(
158 			@PathParam("sha1OrMd5") String sha1OrMd5Hash,
159 			@QueryParam("tag") List<String> tags) throws AuthenticationException {
160 		FileIdentifier identifier = new FileIdentifier(sha1OrMd5Hash);
161 		IsTrueResult result = fileService.isFileTaggedWithAll(identifier, tags.toArray(new String[tags.size()]));
162 		return resultToString(result);
163 	}
164 
165 	/**
166 	 * Allows to query the GRID whether a file is tagged with a set of tags.
167 	 *
168 	 * @param sha1Hash The SHA1 hash (as hexadecimal string) of the file to query.
169 	 * @param md5Hash  The MD5 hash (as hexadecimal string) of the file to query.
170 	 * @return 'true' if the file is tagged with all given tags, 'false' if not, 'null' (HTTP 204 No Content) if it's unkown.
171 	 * @throws AuthenticationException In case of this service requires authentication and the current user session
172 	 *                                 is not authenticated or doesn't have the right to access the service.
173 	 * @RequiredRoles ROLE_RUN_HASH_QUERIES
174 	 */
175 	@GET
176 	@Produces("text/plain")
177 	@Path("/isTaggedWith/{sha1}-{md5}")
178 	public String isFileTaggedWith(
179 			@PathParam("sha1") String sha1Hash, @PathParam("md5") String md5Hash,
180 			@QueryParam("tag") List<String> tags) throws AuthenticationException {
181 		FileIdentifier identifier = new FileIdentifier(sha1Hash, md5Hash);
182 		IsTrueResult result = fileService.isFileTaggedWithAll(identifier, tags.toArray(new String[tags.size()]));
183 		return resultToString(result);
184 	}
185 
186 	/**
187 	 * Returns the file information (containing last modified and tags) on the specified file.
188 	 *
189 	 * @param sha1OrMd5Hash The SHA1 or MD5 hash (as hexadecimal string) of the file to query.
190 	 * @return 'true' if the file is tagged with all given tags, 'false' if not, 'null' (HTTP 204 No Content) if it's unkown.
191 	 * @throws AuthenticationException In case of this service requires authentication and the current user session
192 	 *                                 is not authenticated or doesn't have the right to access the service.
193 	 * @RequiredRoles ROLE_RUN_HASH_QUERIES
194 	 * @dim.example.response com.trendmicro.grid.acl.l0.datatypes.TestContext.newFinalProcessPackageDataSet().getProcessedPackage().getPackageInformation().getPackageFileInformation()
195 	 */
196 	@GET
197 	@Path("/info/{sha1OrMd5}")
198 	public FileInformation getFileInformation(
199 			@PathParam("sha1OrMd5") String sha1OrMd5Hash) throws AuthenticationException {
200 		FileIdentifier identifier = new FileIdentifier(sha1OrMd5Hash);
201 		return convertJpaFileInformation(fileService.getFileInformation(identifier));
202 	}
203 
204 	/**
205 	 * Returns the file information (containing last modified and tags) on the specified file.
206 	 *
207 	 * @param sha1Hash The SHA1 hash (as hexadecimal string) of the file to query.
208 	 * @param md5Hash  The MD5 hash (as hexadecimal string) of the file to query.
209 	 * @return 'true' if the file is tagged with all given tags, 'false' if not, 'null' (HTTP 204 No Content) if it's unkown.
210 	 * @throws AuthenticationException In case of this service requires authentication and the current user session
211 	 *                                 is not authenticated or doesn't have the right to access the service.
212 	 * @RequiredRoles ROLE_RUN_HASH_QUERIES
213 	 * @dim.example.response com.trendmicro.grid.acl.l0.datatypes.TestContext.newFinalProcessPackageDataSet().getProcessedPackage().getPackageInformation().getPackageFileInformation()
214 	 */
215 	@GET
216 	@Path("/info/{sha1}-{md5}")
217 	public FileInformation getFileInformation(
218 			@PathParam("sha1") String sha1Hash, @PathParam("md5") String md5Hash) throws AuthenticationException {
219 		FileIdentifier identifier = new FileIdentifier(sha1Hash, md5Hash);
220 		return convertJpaFileInformation(fileService.getFileInformation(identifier));
221 
222 	}
223 
224 	/**
225 	 * Returns the file information (containing info and attached metadata) on the specified file.
226 	 *
227 	 * @param sha1OrMd5Hash The SHA1 or MD5 hash (as hexadecimal string) of the file to query.
228 	 * @return 'true' if the file is tagged with all given tags, 'false' if not, 'null' (HTTP 204 No Content) if it's unkown.
229 	 * @throws AuthenticationException In case of this service requires authentication and the current user session
230 	 *                                 is not authenticated or doesn't have the right to access the service.
231 	 * @RequiredRoles ROLE_RUN_HASH_QUERIES, ROLE_ACCESS_DETAILS
232 	 * @dim.example.response com.trendmicro.grid.acl.l0.datatypes.TestContext.newDefaultDetails()
233 	 */
234 	@GET
235 	@Path("/details/{sha1OrMd5}")
236 	public FileDetails getFileDetails(
237 			@PathParam("sha1OrMd5") String sha1OrMd5Hash) throws AuthenticationException {
238 		FileIdentifier identifier = new FileIdentifier(sha1OrMd5Hash);
239 		return convertJpaFileDetails(fileService.getFileDetails(identifier));
240 	}
241 
242 	/**
243 	 * Returns the file details (containing info and attached metadata) on the specified file.
244 	 *
245 	 * @param sha1Hash The SHA1 hash (as hexadecimal string) of the file to query.
246 	 * @param md5Hash  The MD5 hash (as hexadecimal string) of the file to query.
247 	 * @return 'true' if the file is tagged with all given tags, 'false' if not, 'null' (HTTP 204 No Content) if it's unkown.
248 	 * @throws AuthenticationException In case of this service requires authentication and the current user session
249 	 *                                 is not authenticated or doesn't have the right to access the service.
250 	 * @RequiredRoles ROLE_RUN_HASH_QUERIES, ROLE_ACCESS_DETAILS
251 	 * @dim.example.response com.trendmicro.grid.acl.l0.datatypes.TestContext.newDefaultDetails()
252 	 */
253 	@GET
254 	@Path("/details/{sha1}-{md5}")
255 	public FileDetails getFileDetails(
256 			@PathParam("sha1") String sha1Hash, @PathParam("md5") String md5Hash) throws AuthenticationException {
257 		FileIdentifier identifier = new FileIdentifier(sha1Hash, md5Hash);
258 		return convertJpaFileDetails(fileService.getFileDetails(identifier));
259 	}
260 }