/** * Compute the representation digest according to MD5 algorithm.<br> * If case this algorithm is the same than the one provided at * instantiation, the computation operation is made with the current stored * computed value and does not require to exhaust entirely the * representation's stream. */ public Digest computeDigest() { return computeDigest(Digest.ALGORITHM_MD5); }
@Override public String getText() throws IOException { String result = null; if (isAvailable()) { if (getSize() == 0) { result = ""; } else { java.io.StringWriter sw = new java.io.StringWriter(); write(sw); result = sw.toString(); } } return result; }
@Override public void write(Writer writer) throws IOException { OutputStream os = IoUtils.getStream(writer, getCharacterSet()); write(os); os.flush(); } }
/** * Exhausts the content of the representation by reading it and silently * discarding anything read. * * @return The number of bytes consumed or -1 if unknown. */ public long exhaust() throws IOException { long result = -1L; if (isAvailable()) { result = IoUtils.exhaust(getStream()); } return result; }
@Get public Representation represent() throws Exception { // Wraps the StringRepresentation DigesterRepresentation result = new DigesterRepresentation( new StringRepresentation("hello, world")); result.exhaust(); // Compute representation's digest. result.setDigest(result.computeDigest()); return result; } }
public static void main(String[] args) throws Exception { ClientResource resource = new ClientResource("http://localhost:8111/"); // The Digester helps computing the digest while reading or writing the // representation's content. DigesterRepresentation rep = new DigesterRepresentation(resource.get()); rep.write(System.out); if (rep.checkDigest()) { System.out.println("\nContent checked."); } else { System.out.println("\nContent not checked."); } }
/** * Check that the digest computed from the representation content and the * digest declared by the representation are the same. It also first checks * that the algorithms are the same.<br> * Since this method relies on the {@link #computeDigest(String)} method, * and since this method reads entirely the representation's stream, user * must take care of the content of the representation in case the latter is * transient. * * {@link #isTransient} * * @param algorithm * The algorithm used to compute the digest to compare with. See * constant values in {@link org.restlet.data.Digest}. * @return True if both digests are not null and equals. */ public boolean checkDigest(String algorithm) { boolean result = false; if (this.algorithm != null && this.algorithm.equals(algorithm)) { result = checkDigest(); } else { Digest digest = getDigest(); if (digest != null) { if (algorithm.equals(digest.getAlgorithm())) { result = digest.equals(computeDigest(algorithm)); } } } return result; }
result = getComputedDigest(); } else if (isAvailable()) { try { java.security.MessageDigest md = java.security.MessageDigest .getInstance(algorithm); java.security.DigestInputStream dis = new java.security.DigestInputStream( getStream(), md); org.restlet.engine.io.IoUtils.exhaust(dis); result = new org.restlet.data.Digest(algorithm, md.digest());
@Override public Reader getReader() throws IOException { return IoUtils.getReader(getStream(), getCharacterSet()); }
/** * Check that the digest computed from the representation content and the * digest declared by the representation are the same.<br> * Since this method relies on the {@link #computeDigest(String)} method, * and since this method reads entirely the representation's stream, user * must take care of the content of the representation in case the latter is * transient. * * {@link #isTransient} * * @return True if both digests are not null and equals. */ public boolean checkDigest() { Digest digest = getDigest(); return (digest != null && digest.equals(getComputedDigest())); }
@Override public void write(WritableByteChannel writableChannel) throws IOException { OutputStream os = IoUtils.getStream(writableChannel); write(os); os.flush(); }
@Override public ReadableByteChannel getChannel() throws IOException { return IoUtils.getChannel(getStream()); }