io.milton.http.json.JsonPropFindHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of milton-server-ce Show documentation
Show all versions of milton-server-ce Show documentation
Milton Community Edition: Supports DAV level 1 and is available on Apache2 license
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 io.milton.http.json;
import io.milton.common.LogUtils;
import io.milton.http.Range;
import io.milton.http.exceptions.BadRequestException;
import io.milton.http.exceptions.NotAuthorizedException;
import io.milton.http.values.ValueAndType;
import io.milton.http.webdav.PropFindPropertyBuilder;
import io.milton.http.webdav.PropFindResponse;
import io.milton.http.webdav.PropertiesRequest;
import io.milton.http.webdav.PropertiesRequest.Property;
import io.milton.http.webdav.WebDavProtocol;
import io.milton.resource.CollectionResource;
import io.milton.resource.PropFindableResource;
import io.milton.resource.Resource;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.util.*;
import java.util.Map.Entry;
import javax.xml.namespace.QName;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author brad
*/
public class JsonPropFindHandler {
private static final Logger log = LoggerFactory.getLogger(JsonPropFindHandler.class);
private final PropFindPropertyBuilder propertyBuilder;
private final Helper helper;
public JsonPropFindHandler(PropFindPropertyBuilder propertyBuilder) {
this.propertyBuilder = propertyBuilder;
helper = new Helper();
}
public void sendContent(PropFindableResource wrappedResource, String encodedUrl, OutputStream out, Range range, Map params, String contentType) throws IOException, NotAuthorizedException, BadRequestException {
log.debug("sendContent: " + encodedUrl);
JsonConfig cfg = new JsonConfig();
cfg.setIgnoreTransientFields(true);
cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
JSON json;
Writer writer = new PrintWriter(out);
String[] arr;
if (propertyBuilder == null) {
if (wrappedResource instanceof CollectionResource) {
List extends Resource> children = Optional.ofNullable(((CollectionResource) wrappedResource).getChildren()).orElse(List.of());
json = JSONSerializer.toJSON(toSimpleList(children), cfg);
} else {
json = JSONSerializer.toJSON(toSimple(wrappedResource), cfg);
}
} else {
// use propfind handler
String sFields = params.get("fields");
Set fields = new HashSet<>();
Map aliases = new HashMap<>();
if (sFields != null && sFields.length() > 0) {
arr = sFields.split(",");
for (String s : arr) {
parseField(s, fields, aliases);
}
}
String sDepth = params.get("depth");
int depth = 1;
if (sDepth != null && sDepth.trim().length() > 0) {
depth = Integer.parseInt(sDepth);
}
String href = encodedUrl.replace("/_DAV/PROPFIND", "");
PropertiesRequest parseResult = new PropertiesRequest(toProperties(fields));
LogUtils.debug(log, "prop builder: ", propertyBuilder.getClass(), "href", href);
List props;
try {
props = propertyBuilder.buildProperties(wrappedResource, depth, parseResult, href);
} catch (URISyntaxException ex) {
throw new RuntimeException("Requested url is not properly encoded: " + href, ex);
}
String where = params.get("where");
filterResults(props, where);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy