Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* acme4j - Java ACME client
*
* Copyright (C) 2016 Richard "Shred" Körber
* http://acme4j.shredzone.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
package org.shredzone.acme4j.toolbox;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.joining;
import static org.shredzone.acme4j.toolbox.AcmeUtils.parseTimestamp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import edu.umd.cs.findbugs.annotations.Nullable;
import org.jose4j.json.JsonUtil;
import org.jose4j.lang.JoseException;
import org.shredzone.acme4j.Identifier;
import org.shredzone.acme4j.Problem;
import org.shredzone.acme4j.Status;
import org.shredzone.acme4j.exception.AcmeNotSupportedException;
import org.shredzone.acme4j.exception.AcmeProtocolException;
/**
* A model containing a JSON result. The content is immutable.
*/
public final class JSON implements Serializable {
private static final long serialVersionUID = 418332625174149030L;
private static final JSON EMPTY_JSON = new JSON(new HashMap<>());
private final String path;
private final Map data;
/**
* Creates a new {@link JSON} root object.
*
* @param data
* {@link Map} containing the parsed JSON data
*/
private JSON(Map data) {
this("", data);
}
/**
* Creates a new {@link JSON} branch object.
*
* @param path
* Path leading to this branch.
* @param data
* {@link Map} containing the parsed JSON data
*/
private JSON(String path, Map data) {
this.path = path;
this.data = data;
}
/**
* Parses JSON from an {@link InputStream}.
*
* @param in
* {@link InputStream} to read from. Will be closed after use.
* @return {@link JSON} of the read content.
*/
public static JSON parse(InputStream in) throws IOException {
try (var reader = new BufferedReader(new InputStreamReader(in, UTF_8))) {
var json = reader.lines().map(String::trim).collect(joining());
return parse(json);
}
}
/**
* Parses JSON from a String.
*
* @param json
* JSON string
* @return {@link JSON} of the read content.
*/
public static JSON parse(String json) {
try {
return new JSON(JsonUtil.parseJson(json));
} catch (JoseException ex) {
throw new AcmeProtocolException("Bad JSON: " + json, ex);
}
}
/**
* Creates a JSON object from a map.
*
* The map's content is deeply copied. Changes to the map won't reflect in the created
* JSON structure.
*
* @param data
* Map structure
* @return {@link JSON} of the map's content.
* @since 3.2.0
*/
public static JSON fromMap(Map data) {
return JSON.parse(JsonUtil.toJson(data));
}
/**
* Returns a {@link JSON} of an empty document.
*
* @return Empty {@link JSON}
*/
public static JSON empty() {
return EMPTY_JSON;
}
/**
* Returns a set of all keys of this object.
*
* @return {@link Set} of keys
*/
public Set keySet() {
return Collections.unmodifiableSet(data.keySet());
}
/**
* Checks if this object contains the given key.
*
* @param key
* Name of the key to check
* @return {@code true} if the key is present
*/
public boolean contains(String key) {
return data.containsKey(key);
}
/**
* Returns the {@link Value} of the given key.
*
* @param key
* Key to read
* @return {@link Value} of the key
*/
public Value get(String key) {
return new Value(
path.isEmpty() ? key : path + '.' + key,
data.get(key));
}
/**
* Returns the {@link Value} of the given key.
*
* @param key
* Key to read
* @return {@link Value} of the key
* @throws AcmeNotSupportedException
* if the key is not present. The key is used as feature name.
*/
public Value getFeature(String key) {
return new Value(
path.isEmpty() ? key : path + '.' + key,
data.get(key)).onFeature(key);
}
/**
* Returns the content as JSON string.
*/
@Override
public String toString() {
return JsonUtil.toJson(data);
}
/**
* Returns the content as unmodifiable Map.
*
* @since 2.8
*/
public Map toMap() {
return Collections.unmodifiableMap(data);
}
/**
* Represents a JSON array.
*/
public static final class Array implements Iterable {
private final String path;
private final List