Revision 5ca9c0d6

b/gss/src/gr/ebs/gss/server/ejb/ExternalAPIBean.java
2207 2207
		if (fileId == null)
2208 2208
			throw new ObjectNotFoundException("No file specified");
2209 2209
		String contentType = mimeType;
2210
		if (StringUtils.isEmpty(mimeType))
2211
			contentType = DEFAULT_MIME_TYPE;
2212 2210

  
2213 2211
		FileHeader file = dao.getEntityById(FileHeader.class, fileId);
2212

  
2213
		// if no mime type or the generic mime type is defined by the client, then try to identify it from the filename extension
2214
		if (StringUtils.isEmpty(mimeType) || "application/octet-stream".equals(mimeType))
2215
			contentType = identifyMimeType(file.getName());
2216

  
2214 2217
		final User owner = dao.getEntityById(User.class, userId);
2215 2218
		if (!file.hasWritePermission(owner))
2216 2219
			throw new InsufficientPermissionsException("You don't have the necessary permissions");
......
2230 2233
	}
2231 2234

  
2232 2235
	/**
2236
	 * Helper method for identifying mime type by examining the filename extension
2237
	 *
2238
	 * @param filename
2239
	 * @return the mime type
2240
	 */
2241
	private String identifyMimeType(String filename) {
2242
		if (filename.indexOf('.') != 1) {
2243
			String extension = filename.substring(filename.lastIndexOf('.'));
2244
			if (".doc".equals(extension))
2245
				return "application/msword";
2246
			else if (".xls".equals(extension))
2247
				return "application/vnd.ms-excel";
2248
			else if (".ppt".equals(extension))
2249
				return "application/vnd.ms-powerpoint";
2250
			else if (".pdf".equals(extension))
2251
				return "application/pdf";
2252
		}
2253
		// when all else fails assign the default mime type
2254
		return DEFAULT_MIME_TYPE;
2255
	}
2256

  
2257
	/**
2233 2258
	 * Helper method to create a new file body and attach it as the current body
2234 2259
	 * of the provided file header.
2235 2260
	 *
......
2246 2271
				FileHeader header, AuditInfo auditInfo, User owner)
2247 2272
			throws FileNotFoundException, QuotaExceededException {
2248 2273
		FileBody body = new FileBody();
2249
		body.setMimeType(mimeType);
2274

  
2275
		// if no mime type or the generic mime type is defined by the client, then try to identify it from the filename extension
2276
		if (StringUtils.isEmpty(mimeType) || "application/octet-stream".equals(mimeType))
2277
			body.setMimeType(identifyMimeType(name));
2278
		else
2279
			body.setMimeType(mimeType);
2250 2280
		body.setAuditInfo(auditInfo);
2251 2281
		body.setFileSize(uploadedFile.length());
2252 2282
		body.setOriginalFilename(name);

Also available in: Unified diff