Skeleton.java
/***************************************************************************
Copyright 2008 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.simpletemplate.interpreter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import net.metanotion.web.HttpStatus;
import net.metanotion.web.HttpValues;
import net.metanotion.web.RequestObject;
import net.metanotion.web.concrete.SimpleHttpValues;
import net.metanotion.scripting.ObjectServer;
import net.metanotion.simpletemplate.Resource;
final class Skeleton implements Resource {
private final Element[] pieces;
private final Long maxExpire;
private final boolean doETag;
private final int hash;
public Skeleton(final Element[] p, final boolean doETag, final Long maxExpire, final String defaultMIMEType) {
this.doETag = doETag;
this.maxExpire = maxExpire;
this.pieces = new Element[p.length];
for(int i=0; i < p.length; i++) {
this.pieces[i] = p[i];
}
this.hash = java.util.Arrays.hashCode(this.pieces);
}
@Override public Object skin(final ObjectServer so, final RequestObject ro) {
final ArrayList out = new ArrayList();
final ArrayList<Map.Entry<String,Object>> headers = new ArrayList<Map.Entry<String,Object>>();
final ArrayList<Map.Entry<String,Object>> cookies = new ArrayList<Map.Entry<String,Object>>();
int status = HttpStatus.OK.codeNumber();
for(final Element e: pieces) {
final Object o = e.eval(so, ro);
if(o instanceof HttpValues) {
final HttpValues hv = (HttpValues) o;
final Iterator<Map.Entry<String,Object>> hs = hv.listHeaders();
final Iterator<Map.Entry<String,Object>> cs = hv.listCookies();
while(hs.hasNext()) { headers.add(hs.next()); }
while(cs.hasNext()) { cookies.add(cs.next()); }
status = hv.getHttpStatus();
out.add(hv.unwrap());
} else {
out.add(o);
}
}
return new SimpleHttpValues(headers, cookies, out, status);
}
@Override public boolean equals(final Object o) {
if(!(o instanceof Skeleton)) { return false; }
return Arrays.equals(pieces, ((Skeleton) o).pieces);
}
@Override public int hashCode() { return hash; }
}