use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class HeaderFunctions method eval.
@Override
public Sequence eval(final Sequence[] args, final HttpRequest request) throws XPathException {
final Sequence result;
if (isCalledAs(qnHeaderNames.getLocalPart())) {
result = new ValueSequence();
for (final String parameterName : request.getHeaderNames()) {
result.add(new StringValue(parameterName));
}
} else if (isCalledAs(qnHeader.getLocalPart())) {
final String headerName = args[0].getStringValue();
if (getSignature().getArgumentCount() == 1) {
result = getHeader(request, headerName, null);
} else if (getSignature().getArgumentCount() == 2) {
final Sequence defaultValues = args[1];
result = getHeader(request, headerName, defaultValues);
} else {
throw new XPathException(this, "Unknown function call: " + getSignature());
}
} else {
throw new XPathException(this, "Unknown function call: " + getSignature());
}
return result;
}
use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class HeaderFunctions method getHeader.
private Sequence getHeader(final HttpRequest request, final String headerName, final Sequence defaultValues) throws XPathException {
final Sequence result;
final String headerValue = request.getHeader(headerName);
if (headerValue == null) {
if (defaultValues != null) {
result = defaultValues;
} else {
result = Sequence.EMPTY_SEQUENCE;
}
} else {
result = new StringValue(headerValue);
}
return result;
}
use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class ParameterFunctions method eval.
@Override
public Sequence eval(final Sequence[] args, final HttpRequest request) throws XPathException {
final Sequence result;
if (isCalledAs(qnParameterNames.getLocalPart())) {
result = new ValueSequence();
for (final String parameterName : request.getParameterNames()) {
result.add(new StringValue(parameterName));
}
} else if (isCalledAs(qnParameter.getLocalPart())) {
final String paramName = args[0].getStringValue();
if (getSignature().getArgumentCount() == 1) {
result = getParameter(request, paramName, null);
} else if (getSignature().getArgumentCount() == 2) {
final Sequence defaultValues = args[1];
result = getParameter(request, paramName, defaultValues);
} else {
throw new XPathException(this, "Unknown function call: " + getSignature());
}
} else {
throw new XPathException(this, "Unknown function call: " + getSignature());
}
return result;
}
use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class RestXqServiceImpl method parseAsString.
private static StringValue parseAsString(final InputStream is, final String encoding) throws IOException {
final String s;
try (final UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(4096)) {
bos.write(is);
s = new String(bos.toByteArray(), encoding);
}
return new StringValue(s);
}
use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class RestXqServiceImpl method extractRequestBody.
@Override
protected Sequence extractRequestBody(final HttpRequest request) throws RestXqServiceException {
// TODO don't use close shield input stream and move parsing of form parameters from HttpServletRequestAdapter into RequestBodyParser
InputStream is;
FilterInputStreamCache cache = null;
try {
// first, get the content of the request
is = new CloseShieldInputStream(request.getInputStream());
if (is.available() <= 0) {
return null;
}
// if marking is not supported, we have to cache the input stream, so we can reread it, as we may use it twice (once for xml attempt and once for string attempt)
if (!is.markSupported()) {
cache = FilterInputStreamCacheFactory.getCacheInstance(() -> {
final Configuration configuration = getBrokerPool().getConfiguration();
return (String) configuration.getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY);
}, is);
is = new CachingFilterInputStream(cache);
}
is.mark(Integer.MAX_VALUE);
} catch (final IOException ioe) {
throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
}
Sequence result = null;
try {
// was there any POST content?
if (is != null && is.available() > 0) {
String contentType = request.getContentType();
// 1) determine if exists mime database considers this binary data
if (contentType != null) {
// strip off any charset encoding info
if (contentType.contains(";")) {
contentType = contentType.substring(0, contentType.indexOf(";"));
}
MimeType mimeType = MimeTable.getInstance().getContentType(contentType);
if (mimeType != null && !mimeType.isXMLType()) {
// binary data
try {
final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), is);
if (binaryValue != null) {
result = new SequenceImpl<>(new BinaryTypedValue(binaryValue));
}
} catch (final XPathException xpe) {
throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, xpe);
}
}
}
if (result == null) {
// 2) not binary, try and parse as an XML document
final DocumentImpl doc = parseAsXml(is);
if (doc != null) {
result = new SequenceImpl<>(new DocumentTypedValue(doc));
}
}
if (result == null) {
String encoding = request.getCharacterEncoding();
// 3) not a valid XML document, return a string representation of the document
if (encoding == null) {
encoding = "UTF-8";
}
try {
// reset the stream, as we need to reuse for string parsing
is.reset();
final StringValue str = parseAsString(is, encoding);
if (str != null) {
result = new SequenceImpl<>(new StringTypedValue(str));
}
} catch (final IOException ioe) {
throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
}
}
}
} catch (IOException e) {
throw new RestXqServiceException(e.getMessage());
} finally {
if (cache != null) {
try {
cache.invalidate();
} catch (final IOException ioe) {
LOG.error(ioe.getMessage(), ioe);
}
}
if (is != null) {
/*
* Do NOT close the stream if its a binary value,
* because we will need it later for serialization
*/
boolean isBinaryType = false;
if (result != null) {
try {
final Type type = result.head().getType();
isBinaryType = (type == Type.BASE64_BINARY || type == Type.HEX_BINARY);
} catch (final IndexOutOfBoundsException ioe) {
LOG.warn("Called head on an empty HTTP Request body sequence", ioe);
}
}
if (!isBinaryType) {
try {
is.close();
} catch (final IOException ioe) {
LOG.error(ioe.getMessage(), ioe);
}
}
}
}
if (result != null) {
return result;
} else {
return Sequence.EMPTY_SEQUENCE;
}
}
Aggregations