@Override public SecretKey getIssuerSecretKey(URI issuerParamsUid) throws CredentialManagerException { if (issuerParamsUid == null) { throw new CredentialManagerException("Issuer parameters UID is null"); } ObjectInputStream objectInput = null; ByteArrayInputStream byteArrayInputStream = null; try { byte[] tokenBytes = this.storage.getIssuerSecretKey(issuerParamsUid); if (tokenBytes == null) { throw new IssuerSecretKeyNotInStorageException( "Issuer secret key with UID: \"" + issuerParamsUid + "\" is not in storage"); } byteArrayInputStream = new ByteArrayInputStream(tokenBytes); objectInput = new ObjectInputStream(byteArrayInputStream); SecretKey issuerSecretKey = (SecretKey) objectInput.readObject(); return issuerSecretKey; } catch (IssuerSecretKeyNotInStorageException ex) { throw ex; } catch (Exception ex) { throw new CredentialManagerException(ex); } finally { // Close the streams. StorageUtil.closeIgnoringException(objectInput); StorageUtil.closeIgnoringException(byteArrayInputStream); } }
@Override public void addValueAndOverwrite(URI uri, byte[] key) throws IOException { try { if (StorageUtil.HasUri(this.file, uri)) { StorageUtil.deleteData(this.file, uri); } this.addValue(uri, key); } catch (Exception e) { throw new IOException(e); } }
@Override public void storeIssuerSecretKey(URI issuerParamsUid, SecretKey issuerSecretKey) throws CredentialManagerException { ByteArrayOutputStream byteArrayOutputStream = null; ObjectOutputStream objectOutput = null; try { byteArrayOutputStream = new ByteArrayOutputStream(); objectOutput = new ObjectOutputStream(byteArrayOutputStream); objectOutput.writeObject(issuerSecretKey); byte[] bytes = byteArrayOutputStream.toByteArray(); this.storage.addIssuerSecret(issuerParamsUid, bytes); } catch (Exception ex) { throw new CredentialManagerException(ex); } finally { // Close the streams. StorageUtil.closeIgnoringException(objectOutput); StorageUtil.closeIgnoringException(byteArrayOutputStream); } }
@Override public URI[] listUris() throws Exception { FileInputStream fis = new FileInputStream(this.file); long fileSize = this.file.length(); if (fileSize < 4) { fis.close(); return new URI[0]; } DataInputStream in = new DataInputStream(new BufferedInputStream(fis)); List<URI> ls = new ArrayList<URI>(); try { while (true) { int n = in.readInt(); byte[] uriBytes = new byte[n]; URI storedUri = StorageUtil.readUriBytes(in, n, uriBytes); ls.add(storedUri); int sizeOfValue = in.readInt(); StorageUtil.skip(in, sizeOfValue); } } catch (EOFException ex) { return ls.toArray(new URI[0]); } }
@Override public void addValue(URI uri, byte[] key) throws IOException { StorageUtil.appendData(this.file, uri, key); }
@Override public byte[] getValue(URI uri) throws Exception { return StorageUtil.getData(this.file, uri); }