01: /*
02: * argun 1.0
03: * Web 2.0 delivery framework
04: * Copyright (C) 2007 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://www.hammurapi.biz
21: * e-Mail: support@hammurapi.biz
22: */
23: package biz.hammurapi.web.util;
24:
25: import java.sql.SQLException;
26: import java.util.ArrayList;
27: import java.util.Collection;
28: import java.util.HashMap;
29: import java.util.Iterator;
30: import java.util.Map;
31:
32: import javax.servlet.http.HttpServletRequest;
33:
34: import biz.hammurapi.config.Context;
35: import biz.hammurapi.config.PropertyParser;
36: import biz.hammurapi.sql.SQLProcessor;
37: import biz.hammurapi.web.FileActions;
38: import biz.hammurapi.web.RequestContext;
39: import biz.hammurapi.web.file.sql.DbFileType;
40: import biz.hammurapi.web.file.sql.FileEngine;
41:
42: /**
43: * Utility class to parse Read Only View Url for a file
44: * @author Pavel Vlasov
45: */
46: public class RoViewUrlParser {
47:
48: /**
49: * Looks up proper file type, parses
50: * @param request Request to construct chained RequestContext
51: * @param fileRecord File record to get values from
52: * @param processor SQL processor to look up proper MIME TYPE
53: * @throws SQLException
54: */
55: public String parse(HttpServletRequest request, Context fileRecord,
56: SQLProcessor processor) throws SQLException {
57: FileEngine engine = new FileEngine(processor);
58: Collection fileTypes = engine.getDbFileType(new ArrayList());
59: Map typeMap = new HashMap();
60: Iterator it = fileTypes.iterator();
61: while (it.hasNext()) {
62: DbFileType dbFileType = (DbFileType) it.next();
63: typeMap.put(dbFileType.getMimeType(), dbFileType);
64: }
65: String template = getViewUrlTemplate((String) fileRecord
66: .get("CONTENT_TYPE"), typeMap);
67: if (template == null) {
68: return null;
69: }
70:
71: RequestContext rc = new RequestContext(request, fileRecord);
72: PropertyParser parser = new PropertyParser(rc, false);
73: return parser.parse(template);
74: }
75:
76: private String getViewUrlTemplate(String mimeType, Map typeMap)
77: throws SQLException {
78: DbFileType fileType = (DbFileType) typeMap.get(mimeType);
79: if (fileType != null && fileType.getRoViewUrl() != null) {
80: return fileType.getRoViewUrl();
81: }
82:
83: int idx = mimeType == null ? -1 : mimeType.indexOf("/");
84:
85: if (idx == -1) {
86: fileType = (DbFileType) typeMap
87: .get(FileActions.ROOT_CONTENT_TYPE);
88: return fileType == null ? null : fileType.getRoViewUrl();
89: }
90:
91: return getViewUrlTemplate(mimeType.substring(0, idx), typeMap);
92: }
93:
94: }
|