/*   
 * Copyright 2009 O'Reilly Media, Inc
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oreilly.rdf.tenuki.restlet;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restlet.data.MediaType;
import org.restlet.resource.OutputRepresentation;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.shared.Lock;

public class WholeModelRepresentation extends OutputRepresentation {
	private Log log = LogFactory.getLog(WholeModelRepresentation.class);
	private Model model;
	private Lock lock;

	public WholeModelRepresentation(Model model, Lock lock) {
		super(MediaType.APPLICATION_RDF_XML);
		this.model = model;
		this.lock = lock;
	}

	@Override
	public void write(OutputStream output) throws IOException {
		lock.enterCriticalSection(Lock.READ);
		try {
			model.write(output);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException(e);
		} finally {
			lock.leaveCriticalSection();
		}

	}

}

@@ -648,16 +648,54 @@
 stlet;%0A%0A
+import java.io.ByteArrayOutputStream;%0A
 import j
@@ -742,16 +742,128 @@
 tStream;
+%0Aimport java.math.BigInteger;%0Aimport java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;
 %0A%0Aimport
@@ -976,16 +976,45 @@
 iaType;%0A
+import org.restlet.data.Tag;%0A
 import o
@@ -1285,39 +1285,22 @@
 ate 
-Model model;%0A%09private Lock lock
+byte%5B%5D content
 ;%0A%0A%09
@@ -1403,123 +1403,63 @@
 ;%0A%09%09
-this.model = model;%0A%09%09this.lock = lock;%0A%09%7D%0A%0A%09@Override%0A%09public void write(OutputStream output) throws IOException %7B
+ByteArrayOutputStream bo = new ByteArrayOutputStream();
 %0A%09%09l
@@ -1522,14 +1522,10 @@
 ite(
+b
 o
-utput
 );%0A%09
@@ -1650,15 +1650,460 @@
 ();%0A%09%09%7D%0A
+%09%09content = bo.toByteArray();%0A%09%09setTag(calculateTag());%0A%09%7D%0A%09%0A%09private Tag calculateTag() %7B%0A%09%09try %7B%0A%09%09%09MessageDigest sha1 = MessageDigest.getInstance(%22SHA1%22);%0A%09%09%09byte%5B%5D digest = sha1.digest(content);%0A%09%09%09return new Tag( new BigInteger(1, digest).toString(16), false);%0A%09%09%7D catch (NoSuchAlgorithmException e) %7B%0A%09%09%09throw new RuntimeException(e);%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public void write(OutputStream output) throws IOException %7B%0A%09%09output.write(content);
 %0A%09%7D%0A%0A%7D%0A

Generate ETAGs