|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + * info@exist-db.org |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.xquery.modules.request; |
| 23 | + |
| 24 | +import org.exist.http.servlets.RequestWrapper; |
| 25 | +import org.exist.xquery.*; |
| 26 | +import org.exist.xquery.value.*; |
| 27 | + |
| 28 | +import javax.annotation.Nonnull; |
| 29 | +import java.util.Enumeration; |
| 30 | + |
| 31 | +import static org.exist.xquery.FunctionDSL.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * Implements request:attribute(), request:attribute-names(), |
| 35 | + * request:attribute-map(), and request:set-attribute(). |
| 36 | + */ |
| 37 | +public class AttributeFunctions extends AbstractRequestFunction { |
| 38 | + |
| 39 | + private static final String FS_ATTRIBUTE_NAME = "attribute"; |
| 40 | + public static final FunctionSignature[] FS_ATTRIBUTE = functionSignatures( |
| 41 | + RequestModule.qname(FS_ATTRIBUTE_NAME), |
| 42 | + "Returns the value of the named request attribute.", |
| 43 | + returnsOptMany(Type.ITEM, "the attribute value, or empty if not found"), |
| 44 | + arities( |
| 45 | + arity( |
| 46 | + param("name", Type.STRING, "the attribute name") |
| 47 | + ), |
| 48 | + arity( |
| 49 | + param("name", Type.STRING, "the attribute name"), |
| 50 | + param("default", Type.ITEM, "default value if attribute is not present") |
| 51 | + ) |
| 52 | + ) |
| 53 | + ); |
| 54 | + |
| 55 | + private static final String FS_ATTRIBUTE_NAMES_NAME = "attribute-names"; |
| 56 | + public static final FunctionSignature FS_ATTRIBUTE_NAMES = functionSignature( |
| 57 | + RequestModule.qname(FS_ATTRIBUTE_NAMES_NAME), |
| 58 | + "Returns the names of all request attributes.", |
| 59 | + returnsOptMany(Type.STRING, "the attribute names") |
| 60 | + ); |
| 61 | + |
| 62 | + private static final String FS_ATTRIBUTE_MAP_NAME = "attribute-map"; |
| 63 | + public static final FunctionSignature FS_ATTRIBUTE_MAP = functionSignature( |
| 64 | + RequestModule.qname(FS_ATTRIBUTE_MAP_NAME), |
| 65 | + "Returns all request attributes as a map.", |
| 66 | + returns(Type.MAP, "a map of attribute names to values") |
| 67 | + ); |
| 68 | + |
| 69 | + private static final String FS_SET_ATTRIBUTE_NAME = "set-attribute"; |
| 70 | + public static final FunctionSignature FS_SET_ATTRIBUTE = functionSignature( |
| 71 | + RequestModule.qname(FS_SET_ATTRIBUTE_NAME), |
| 72 | + "Sets a request attribute. Returns the empty sequence.", |
| 73 | + returns(Type.EMPTY_SEQUENCE, "empty sequence"), |
| 74 | + params( |
| 75 | + param("name", Type.STRING, "the attribute name"), |
| 76 | + param("value", Type.ITEM, "the attribute value") |
| 77 | + ) |
| 78 | + ); |
| 79 | + |
| 80 | + public AttributeFunctions(final XQueryContext context, final FunctionSignature signature) { |
| 81 | + super(context, signature); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected Sequence eval(final Sequence[] args, @Nonnull final RequestWrapper request) |
| 86 | + throws XPathException { |
| 87 | + if (isCalledAs(FS_ATTRIBUTE_NAME)) { |
| 88 | + return getAttribute(args, request); |
| 89 | + |
| 90 | + } else if (isCalledAs(FS_ATTRIBUTE_NAMES_NAME)) { |
| 91 | + return getAttributeNames(request); |
| 92 | + |
| 93 | + } else if (isCalledAs(FS_ATTRIBUTE_MAP_NAME)) { |
| 94 | + return getAttributeMap(request); |
| 95 | + |
| 96 | + } else if (isCalledAs(FS_SET_ATTRIBUTE_NAME)) { |
| 97 | + return setAttribute(args, request); |
| 98 | + |
| 99 | + } else { |
| 100 | + throw new XPathException(this, "Unknown function: " + getSignature()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private Sequence getAttribute(final Sequence[] args, final RequestWrapper request) |
| 105 | + throws XPathException { |
| 106 | + final String name = args[0].getStringValue(); |
| 107 | + final Object value = request.getAttribute(name); |
| 108 | + |
| 109 | + if (value == null) { |
| 110 | + if (args.length > 1) { |
| 111 | + return args[1]; |
| 112 | + } |
| 113 | + return Sequence.EMPTY_SEQUENCE; |
| 114 | + } |
| 115 | + |
| 116 | + return XPathUtil.javaObjectToXPath(value, context, this); |
| 117 | + } |
| 118 | + |
| 119 | + private Sequence getAttributeNames(final RequestWrapper request) { |
| 120 | + final Enumeration<String> names = request.getAttributeNames(); |
| 121 | + if (!names.hasMoreElements()) { |
| 122 | + return Sequence.EMPTY_SEQUENCE; |
| 123 | + } |
| 124 | + |
| 125 | + final ValueSequence result = new ValueSequence(); |
| 126 | + while (names.hasMoreElements()) { |
| 127 | + result.add(new StringValue(this, names.nextElement())); |
| 128 | + } |
| 129 | + return result; |
| 130 | + } |
| 131 | + |
| 132 | + private Sequence getAttributeMap(final RequestWrapper request) throws XPathException { |
| 133 | + final MapType map = new MapType(this, context); |
| 134 | + final Enumeration<String> names = request.getAttributeNames(); |
| 135 | + |
| 136 | + while (names.hasMoreElements()) { |
| 137 | + final String name = names.nextElement(); |
| 138 | + final Object value = request.getAttribute(name); |
| 139 | + if (value != null) { |
| 140 | + map.add(new StringValue(this, name), |
| 141 | + XPathUtil.javaObjectToXPath(value, context, this)); |
| 142 | + } |
| 143 | + } |
| 144 | + return map; |
| 145 | + } |
| 146 | + |
| 147 | + private Sequence setAttribute(final Sequence[] args, final RequestWrapper request) |
| 148 | + throws XPathException { |
| 149 | + final String name = args[0].getStringValue(); |
| 150 | + final Sequence value = args[1]; |
| 151 | + |
| 152 | + // Store the XQuery value as a Java object on the request |
| 153 | + if (value.isEmpty()) { |
| 154 | + request.removeAttribute(name); |
| 155 | + } else if (value.getItemCount() == 1) { |
| 156 | + request.setAttribute(name, value.getStringValue()); |
| 157 | + } else { |
| 158 | + // Multi-valued: store as string for now (most common use case) |
| 159 | + request.setAttribute(name, value.getStringValue()); |
| 160 | + } |
| 161 | + |
| 162 | + return Sequence.EMPTY_SEQUENCE; |
| 163 | + } |
| 164 | +} |
0 commit comments