![JAR search and dependency download from the Maven repository](/logo.png)
com.virgilsecurity.sdk.client.VirgilCardClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk Show documentation
Show all versions of sdk Show documentation
Virgil is a stack of security libraries (ECIES with Crypto Agility wrapped in Virgil Cryptogram) and all the necessary infrastructure to enable seamless, end-to-end encryption for any application, platform or device.
Learn about Virgil Java/Android SDK https://virgilsecurity.com/api-docs/java-android/quickstart
/*
* Copyright (c) 2015-2019, Virgil Security, Inc.
*
* Lead Maintainer: Virgil Security Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* (1) Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* (3) Neither the name of virgil nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.virgilsecurity.sdk.client;
import com.google.gson.annotations.SerializedName;
import com.virgilsecurity.sdk.cards.model.RawSignedModel;
import com.virgilsecurity.sdk.client.exceptions.VirgilCardIsOutdatedException;
import com.virgilsecurity.sdk.client.exceptions.VirgilCardServiceException;
import com.virgilsecurity.sdk.client.exceptions.VirgilServiceException;
import com.virgilsecurity.sdk.exception.EmptyArgumentException;
import com.virgilsecurity.sdk.exception.NullArgumentException;
import com.virgilsecurity.sdk.utils.ConvertionUtils;
import com.virgilsecurity.sdk.utils.Tuple;
import java.io.ByteArrayInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* The {@link VirgilCardClient} class represents a Virgil Security service client and contains all
* methods for interaction with server.
*/
public class VirgilCardClient implements CardClient {
private static final Logger LOGGER = Logger.getLogger(VirgilCardClient.class.getName());
private URL serviceUrl;
private HttpClient httpClient;
/**
* Create a new instance of {@code CardClient}.
*/
public VirgilCardClient() {
this("https://api.virgilsecurity.com/card/v5/");
}
/**
* Create a new instance of {@code CardClient}.
*
* @param serviceUrl
* the service url to fire requests to
*/
public VirgilCardClient(String serviceUrl) {
try {
this.serviceUrl = new URL(serviceUrl);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("CardClient -> 'serviceUrl' has wrong format");
}
httpClient = new HttpClient();
}
/**
* Create a new instance of {@code CardClient}.
*
* @param serviceUrl
* the service url to fire requests to
*/
public VirgilCardClient(URL serviceUrl) {
this.serviceUrl = serviceUrl;
httpClient = new HttpClient();
}
/**
* Get card from Virgil Services by specified identifier.
*
* @param cardId
* the card identifier.
* @param token
* token to authorize the request.
* @return the card loaded from Virgil Cards service.
* @throws VirgilServiceException
* if service call failed
*/
public Tuple getCard(String cardId, String token)
throws VirgilServiceException {
try {
URL url = new URL(serviceUrl, cardId);
return new Tuple<>(httpClient.execute(url, "GET", token, null, RawSignedModel.class), false);
} catch (VirgilCardIsOutdatedException e) {
LOGGER.fine("Outdated Card is received");
return new Tuple<>(e.getCardModel(), true);
} catch (VirgilServiceException e) {
LOGGER.log(Level.SEVERE, "Some service issue occurred during request executing", e);
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Some issue occurred during request executing", e);
throw new VirgilCardServiceException(e);
}
}
/**
* Gets http client that is used to fire requests.
*
* @return the http client
*/
public HttpClient getHttpClient() {
return httpClient;
}
/**
* Gets service url that is used to fire requests to.
*
* @return the service url
*/
public URL getServiceUrl() {
return serviceUrl;
}
/**
* Publishes card in Virgil Cards service.
*
* @param rawCard
* raw signed model of card to be published.
* @param token
* token to authorize the request.
* @return the {@link RawSignedModel} of the Card that is published to Virgil Cards service.
* @throws VirgilServiceException
* if an error occurred while publishing Card.
*/
public RawSignedModel publishCard(RawSignedModel rawCard, String token)
throws VirgilServiceException {
try {
URL url = serviceUrl;
String body = rawCard.exportAsJson();
return httpClient.execute(url, "POST", token,
new ByteArrayInputStream(ConvertionUtils.toBytes(body)), RawSignedModel.class);
} catch (VirgilServiceException e) {
LOGGER.log(Level.SEVERE, "Some service issue occurred during request executing", e);
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Some issue occurred during request executing", e);
throw new VirgilCardServiceException(e);
}
}
/**
* Search cards Virgil Services by specified identity.
*
* @param identity
* the identity for search.
* @param token
* token to authorize the request.
* @return A list of found cards.
* @throws VirgilServiceException
* if service call failed
*/
public List searchCards(String identity, String token)
throws VirgilServiceException {
if (identity == null) {
throw new NullArgumentException("CardClient -> 'identity' should not be null");
}
if (identity.isEmpty()) {
throw new EmptyArgumentException("CardClient -> 'identity' should not be empty");
}
return searchCards(Arrays.asList(identity), token);
}
/**
* Search cards Virgil Services by specified identity.
*
* @param identities
* the identity for search.
* @param token
* token to authorize the request.
* @return A list of found cards.
* @throws VirgilServiceException
* if service call failed
*/
public List searchCards(Collection identities, String token)
throws VirgilServiceException {
if (identities == null) {
throw new NullArgumentException("CardClient -> 'identities' should not be null");
}
if (identities.isEmpty()) {
throw new EmptyArgumentException("CardClient -> 'identities' should not be empty");
}
try {
URL url = new URL(serviceUrl, "actions/search");
SearchCardsRequestData requestData = new SearchCardsRequestData(identities);
String body = ConvertionUtils.getGson().toJson(requestData);
RawSignedModel[] cardModels = httpClient.execute(url, "POST", token,
new ByteArrayInputStream(ConvertionUtils.toBytes(body)), RawSignedModel[].class);
return Arrays.asList(cardModels);
} catch (VirgilServiceException e) {
LOGGER.log(Level.SEVERE, "Some service issue occurred during request executing", e);
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Some issue occurred during request executing", e);
throw new VirgilCardServiceException(e);
}
}
/**
* Sets http client that is used to fire requests.
*
* @param httpClient
* the http client
*/
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
private class SearchCardsRequestData {
@SerializedName("identities")
private List identities;
public SearchCardsRequestData() {
this(Collections.EMPTY_LIST);
}
public SearchCardsRequestData(Collection identities) {
this.identities = new ArrayList<>(identities);
}
public List getIdentities() {
return identities;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy