001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.extension;
016:
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Set;
023: import org.apache.commons.collections.map.LinkedMap;
024:
025: /**
026: * A data structure for holding information about external resources. Different
027: * files are grouped by group names. Every file has its specific content-type. Possible
028: * to query by file name if a file is allowed to be loaded.
029: *
030: * @author "Toomas Römer" <toomas@webmedia.ee>
031: */
032: public class ExternalResource {
033: private Map groupsByName = new HashMap();
034: private Map allowedFiles = new HashMap();
035:
036: public void addGroup(FileGroup fileGroup) {
037: if (fileGroup == null)
038: return;
039:
040: // add files to the allowed list with the content type
041: for (Iterator iter = fileGroup.getFiles().iterator(); iter
042: .hasNext();) {
043: allowedFiles.put(iter.next(), fileGroup.getContentType());
044: }
045:
046: // adding files by group names
047: if (fileGroup.getName() == null)
048: return;
049:
050: Map group = (Map) groupsByName.get(fileGroup.getName());
051: if (group == null) { // no group by this name, lets create one
052: group = new LinkedMap();
053: groupsByName.put(fileGroup.getName(), group);
054: }
055: for (Iterator iter = fileGroup.getFiles().iterator(); iter
056: .hasNext();) {
057: group.put(iter.next(), fileGroup.getContentType());
058: }
059: }
060:
061: /**
062: * Returns a Set of available group names.
063: * @return a Set of available group names.
064: */
065: public Set getGroupNames() {
066: return groupsByName.keySet();
067: }
068:
069: /**
070: * Returns a Map of filenames paired with respective content types.
071: * @param name the name of the group.
072: * @return Map of filenames paired with respective content types.
073: */
074: public Map getGroupByName(String name) {
075: return (Map) groupsByName.get(name);
076: }
077:
078: /**
079: * Returns true if the fileName is in the allowed list.
080: * @param fileName the name of the file.
081: * @return true if the file is in the allowed list otherwise false.
082: */
083: public boolean isAllowedFile(String fileName) {
084: return allowedFiles.containsKey(fileName);
085: }
086:
087: /**
088: * Returns the content-type of the file. Returns null, if no such file is in the allowed list.
089: * @param fileName the name of the file.
090: * @return the content-type of the file.
091: */
092: public String getContentType(String fileName) {
093: return (String) allowedFiles.get(fileName);
094: }
095:
096: static class FileGroup {
097: private String name;
098: private String contentType;
099: private List files;
100:
101: public FileGroup() {
102: files = new ArrayList();
103: }
104:
105: public FileGroup(String name) {
106: this ();
107: this .name = name;
108: }
109:
110: public String getContentType() {
111: return contentType;
112: }
113:
114: public void setContentType(String contentType) {
115: this .contentType = contentType;
116: }
117:
118: public List getFiles() {
119: return files;
120: }
121:
122: public void addFile(String file) {
123: this .files.add(file);
124: }
125:
126: public String getName() {
127: return name;
128: }
129:
130: public void setName(String name) {
131: this.name = name;
132: }
133: }
134: }
|