org.apache.chemistry.opencmis.commons.impl.endpoints.CmisEndpointsDocumentHelper Maven / Gradle / Ivy
/*
* 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 org.apache.chemistry.opencmis.commons.impl.endpoints;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.chemistry.opencmis.commons.endpoints.CmisAuthentication;
import org.apache.chemistry.opencmis.commons.endpoints.CmisEndpoint;
import org.apache.chemistry.opencmis.commons.endpoints.CmisEndpointsDocument;
import org.apache.chemistry.opencmis.commons.impl.IOUtils;
import org.apache.chemistry.opencmis.commons.impl.json.JSONArray;
import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParseException;
import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParser;
/**
* CMIS endpoints document helper methods.
*/
public class CmisEndpointsDocumentHelper {
// -- read --
public static CmisEndpointsDocument read(URL url) throws IOException, JSONParseException {
if (url == null) {
throw new IllegalArgumentException("URL is null!");
}
InputStream stream = url.openStream();
try {
return read(stream);
} finally {
IOUtils.closeQuietly(stream);
}
}
public static CmisEndpointsDocument read(File file) throws IOException, JSONParseException {
if (file == null) {
throw new IllegalArgumentException("File is null!");
}
InputStream stream = new BufferedInputStream(new FileInputStream(file));
try {
return read(stream);
} finally {
IOUtils.closeQuietly(stream);
}
}
public static CmisEndpointsDocument read(InputStream in) throws IOException, JSONParseException {
if (in == null) {
throw new IllegalArgumentException("InputStream is null!");
}
return read(new InputStreamReader(in, "UTF-8"));
}
public static CmisEndpointsDocument read(Reader in) throws IOException, JSONParseException {
if (in == null) {
throw new IllegalArgumentException("Reader is null!");
}
JSONParser parser = new JSONParser();
return convert(parser.parse(in));
}
public static CmisEndpointsDocument read(String in) throws JSONParseException {
if (in == null) {
throw new IllegalArgumentException("String is null!");
}
JSONParser parser = new JSONParser();
return convert(parser.parse(in));
}
private static CmisEndpointsDocument convert(Object obj) throws JSONParseException {
if (!(obj instanceof JSONObject)) {
throw new IllegalArgumentException("JSON is not a CMIS Endpoint Document!");
}
return convertEndpointsDocument((JSONObject) obj);
}
private static CmisEndpointsDocument convertEndpointsDocument(JSONObject json) {
CmisEndpointsDocumentImpl result = new CmisEndpointsDocumentImpl();
for (Map.Entry entry : json.entrySet()) {
if (CmisEndpointsDocument.KEY_ENDPOINTS.equals(entry.getKey()) && (entry.getValue() instanceof JSONArray)) {
result.put(entry.getKey(), convertEndpoints((JSONArray) entry.getValue()));
} else {
result.put(entry.getKey(), entry.getValue());
}
}
return result;
}
private static List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy