001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.html;
030:
031: import com.caucho.util.Base64;
032:
033: import java.io.*;
034:
035: import javax.faces.application.*;
036: import javax.faces.context.*;
037: import javax.faces.render.*;
038:
039: public class ResponseStateManagerImpl extends ResponseStateManager {
040: public void writeState(FacesContext context, Object state)
041: throws IOException {
042: if (Object[].class.isAssignableFrom(state.getClass())) {
043: String value = encode(((Object[]) state)[0]);
044:
045: ResponseWriter rw = context.getResponseWriter();
046:
047: rw.startElement("input", null);
048:
049: rw.writeAttribute("type", "hidden", null);
050: rw.writeAttribute("name", VIEW_STATE_PARAM, null);
051: rw.writeAttribute("value", value, null);
052:
053: rw.endElement("input");
054:
055: rw.write("\n");
056: } else {
057: throw new IllegalArgumentException();
058: }
059: }
060:
061: @Deprecated
062: public void writeState(FacesContext context,
063: StateManager.SerializedView state) throws IOException {
064: }
065:
066: /**
067: * @Since 1.2
068: */
069: public Object getState(FacesContext context, String viewId) {
070: ExternalContext extContext = context.getExternalContext();
071:
072: String data = extContext.getRequestParameterMap().get(
073: VIEW_STATE_PARAM);
074:
075: if (data.charAt(0) == '!') {
076: return new Object[] { data.substring(1), null };
077: } else {
078: return new Object[] { Base64.decodeToByteArray(data), null };
079: }
080: }
081:
082: @Deprecated
083: public Object getTreeStructureToRestore(FacesContext context,
084: String viewId) {
085: return null;
086: }
087:
088: @Deprecated
089: public Object getComponentStateToRestore(FacesContext context) {
090: return null;
091: }
092:
093: /**
094: * Since 1.2
095: */
096: public boolean isPostback(FacesContext context) {
097: ExternalContext extContext = context.getExternalContext();
098:
099: return extContext.getRequestParameterMap().containsKey(
100: VIEW_STATE_PARAM);
101: }
102:
103: private String encode(Object obj) {
104: if (byte[].class.isAssignableFrom(obj.getClass())) {
105:
106: return Base64.encodeFromByteArray((byte[]) obj);
107: } else if (obj instanceof String) {
108:
109: return "!" + obj.toString();
110: } else {
111: throw new IllegalArgumentException();
112: }
113: }
114: }
|