com.inrupt.client.accessgrant.AccessGrant Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of inrupt-client-accessgrant Show documentation
Show all versions of inrupt-client-accessgrant Show documentation
Access Grant support for the Inrupt Client Libraries.
/*
* Copyright Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.inrupt.client.accessgrant;
import static com.inrupt.client.accessgrant.Utils.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.inrupt.client.spi.JsonService;
import com.inrupt.client.spi.ServiceProvider;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
/**
* An Access Grant abstraction, for use when interacting with Solid resources.
*/
public class AccessGrant extends AccessCredential {
private static final Set supportedTypes = getSupportedTypes();
private static final JsonService jsonService = ServiceProvider.getJsonService();
private final URI accessRequest;
/**
* Read a verifiable presentation as an AccessGrant.
*
* @param identifier the credential identifier
* @param credential the serialized form of an Access Grant
* @param data the user-managed data associated with the credential
* @param metadata the server-managed data associated with the credential
*/
protected AccessGrant(final URI identifier, final String credential, final CredentialData data,
final CredentialMetadata metadata) {
super(identifier, credential, data, metadata);
this.accessRequest = data.getAccessRequest();
}
/**
* Get the corresponding access request identifier.
*
* @return the access request identifier, may be {@code null}
*/
public URI getAccessRequest() {
return accessRequest;
}
/**
* Create an AccessGrant object from a serialized form.
*
* @param serialization the serialized access grant
* @return a parsed access grant
*/
public static AccessGrant of(final String serialization) {
try {
return parse(serialization);
} catch (final IOException ex) {
throw new IllegalArgumentException("Unable to read access grant", ex);
}
}
/**
* Create an AccessGrant object from a serialized form.
*
* @param serialization the serialized access grant
* @return a parsed access grant
*/
public static AccessGrant of(final InputStream serialization) {
try {
return of(IOUtils.toString(serialization, UTF_8));
} catch (final IOException ex) {
throw new IllegalArgumentException("Unable to read access grant", ex);
}
}
static Set getSupportedTypes() {
final Set types = new HashSet<>();
types.add("SolidAccessGrant");
types.add("vc:SolidAccessGrant");
types.add("http://www.w3.org/ns/solid/vc#SolidAccessGrant");
return Collections.unmodifiableSet(types);
}
static AccessGrant parse(final String serialization) throws IOException {
try (final InputStream in = new ByteArrayInputStream(serialization.getBytes())) {
// TODO process as JSON-LD
final Map data = jsonService.fromJson(in,
new HashMap(){}.getClass().getGenericSuperclass());
final List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy