package com.jmedved.sample.windays11.helloblob;

import java.io.IOException;
import java.net.URI;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.soyatec.windowsazure.blob.BlobStorageClient;
import org.soyatec.windowsazure.blob.IBlobContainer;
import org.soyatec.windowsazure.blob.IBlobProperties;
import org.soyatec.windowsazure.blob.IBlockBlob;
import org.soyatec.windowsazure.blob.internal.BlobContents;
import org.soyatec.windowsazure.blob.internal.BlobProperties;
import org.soyatec.windowsazure.blob.internal.ContainerAccessControl;
import org.soyatec.windowsazure.blob.io.BlobMemoryStream;

/**
 * Servlet implementation class HelloBlobServlet
 */
public class HelloBlobServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	public static final String ACCOUNT_NAME = ?;
	public static final String ACCOUNT_KEY = ?;
	public static final String BLOB_CONTAINER = "myblob";
	public static final String BLOB_NAME = "millis.txt";

	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloBlobServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action").toString();
		
		if (action.equalsIgnoreCase("Write")) {

        	BlobStorageClient blobStorageClient = BlobStorageClient.create(URI.create("http://blob.core.windows.net/"), false, ACCOUNT_NAME, ACCOUNT_KEY);
        	IBlobContainer blobContainer = blobStorageClient.getBlobContainer(BLOB_CONTAINER);
			if (blobContainer.isContainerExist() == false) {
        		blobContainer = blobStorageClient.createContainer(BLOB_CONTAINER);
			}
    		blobContainer.setAccessControl(ContainerAccessControl.Public);
        	
			String text = Long.toString(Calendar.getInstance().getTimeInMillis());
			if (blobContainer.isBlobExist(BLOB_NAME)) {
				blobContainer.deleteBlob(BLOB_NAME);
			}
        	blobContainer.createBlockBlob(new BlobProperties(BLOB_NAME), new BlobContents(text.getBytes()));

			response.setContentType("text/html");
			response.getWriter().println("<html><body>Write " + text + ".</body></html>");

		} else {
		
        	BlobStorageClient blobStorageClient = BlobStorageClient.create(URI.create("http://blob.core.windows.net/"), false, ACCOUNT_NAME, ACCOUNT_KEY);
        	IBlobContainer myContainer = blobStorageClient.getBlobContainer(BLOB_CONTAINER);
        	
			BlobMemoryStream blobStream = new BlobMemoryStream();
			IBlockBlob blob = myContainer.getBlockBlobReference(BLOB_NAME);
			blob.getContents(blobStream);
			String text = new String(blobStream.getBytes());

			response.setContentType("text/html");
			response.getWriter().println("<html><body>Read: " + text + ".</body></html>");
			
		}

	}

}
