001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.session.Session;
016: import org.wings.session.SessionManager;
017: import org.wings.resource.HttpHeader;
018:
019: import java.io.Serializable;
020: import java.util.Collection;
021: import java.util.Map;
022:
023: /*
024: * Diese Klasse ist nur ein Wrapper, um Eingabestroeme von Grafiken mit dem
025: * ExternalizeManager mit der richtigen Endung und ohne Umweg einer neuen
026: * Codierung (die z.B. keine Transparenz unterstuetzt) uebers WWW zugreifbar zu
027: * machen. Zugleich muss diese Klasse aber auch zu der API der Componenten
028: * passen, also ein Image bzw. ImageIcon sein. ImageIcon ist einfacher zu
029: * benutzen und implementiert schon alles was benoetigt wird...
030: */
031:
032: /**
033: * Wrapper to decorate various resource (i.e. images, files, scripts) with
034: * a MIME type and make it accessible to the client via HTTP and the
035: * {@link org.wings.externalizer.ExternalizeManager}.
036: *
037: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
038: * @author <a href="mailto:H.Zeller@acm.org">Henner Zeller</a>
039: */
040: public abstract class Resource implements Serializable, URLResource,
041: Renderable {
042: /**
043: * a serializable class is supposed to have this ID.
044: */
045: private static final long serialVersionUID = 0x200L;
046:
047: protected String id;
048:
049: protected String extension;
050:
051: protected String mimeType;
052:
053: protected Collection<HttpHeader> headers;
054:
055: protected Resource(String extension, String mimeType) {
056: this .extension = extension;
057: this .mimeType = mimeType;
058: }
059:
060: protected Resource() {
061: }
062:
063: public int getLength() {
064: return -1;
065: }
066:
067: public String getExtension() {
068: return extension;
069: }
070:
071: /**
072: * Returns the mime type of this resource.
073: */
074: public String getMimeType() {
075: return mimeType;
076: }
077:
078: public void setHeaders(Collection<HttpHeader> headers) {
079: this .headers = headers;
080: }
081:
082: public Collection<HttpHeader> getHeaders() {
083: return headers;
084: }
085:
086: public String getId() {
087: return id;
088: }
089:
090: public abstract SimpleURL getURL();
091:
092: public String toString() {
093: return getId();
094: }
095:
096: public Session getSession() {
097: return SessionManager.getSession();
098: }
099:
100: /**
101: * Internal structure to manage HTTP headers which should be delivered with a resource to the client.
102: */
103: public static final class HeaderEntry implements HttpHeader,
104: Serializable {
105: /**
106: * a serializable class is supposed to have this ID.
107: */
108: private static final long serialVersionUID = 0x200L;
109:
110: private final String key;
111: private Serializable value;
112:
113: /**
114: * Create new entry.
115: */
116: public HeaderEntry(String k, Serializable v) {
117: key = k;
118: value = v;
119: }
120:
121: public String getKey() {
122: return key;
123: }
124:
125: public Object getValue() {
126: return value;
127: }
128:
129: public Object setValue(Object newValue) {
130: Object oldValue = value;
131: value = (Serializable) newValue;
132: return oldValue;
133: }
134:
135: public boolean equals(Object o) {
136: if (!(o instanceof Map.Entry))
137: return false;
138: Map.Entry e = (Map.Entry) o;
139: Object k1 = getKey();
140: Object k2 = e.getKey();
141: if (k1 == k2 || (k1 != null && k1.equals(k2))) {
142: Object v1 = getValue();
143: Object v2 = e.getValue();
144: if (v1 == v2 || (v1 != null && v1.equals(v2))) {
145: return true;
146: }
147: }
148: return false;
149: }
150:
151: public int hashCode() {
152: return (key == null ? 0 : key.hashCode())
153: ^ (value == null ? 0 : value.hashCode());
154: }
155:
156: public String toString() {
157: return getKey() + "=" + getValue();
158: }
159:
160: }
161:
162: }
|