
package Servlet;

import Bean.*;

import java.io.*;
import java.util.Iterator;
import java.util.List;

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.openstack4j.model.common.Payload;
import org.openstack4j.model.common.Payloads;

import java.util.List;
import java.lang.reflect.Type;
import java.awt.Component;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ibm.watson.developer_cloud.service.WatsonService;
import com.ibm.watson.developer_cloud.text_to_speech.v1.model.Voice;
import com.ibm.watson.developer_cloud.util.ResponseUtil;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech;
import javax.servlet.annotation.WebServlet;

@WebServlet(urlPatterns = {"/Upload"})
public class TexttospeechObjectStorage extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
			ObjectStorageConnector connect = new ObjectStorageConnector();
			String filename = null;
            Payload upfile = null;

			TexttoSpeechConnector connector = new TexttoSpeechConnector();      
  			TextToSpeech service = new TextToSpeech();
  			service.setUsernameAndPassword(connector.getUsername(),connector.getPassword());
        
        	String text = request.getParameter("inputText");
        	String format = "audio/wav";
			InputStream speech = service.synthesize(text, format);
            //
            upfile = Payloads.create(speech);
            //upload to object storage
            connect.uploadFile("sample", filename, upfile);
                   
               
			response.sendRedirect("texttospeechstoragehome.jsp");
        }catch(Exception e){
			e.printStackTrace(System.err);
		}
    }

  
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

@@ -1671,16 +1671,58 @@
 ctor();%0A
+%09%09%09connect.createContainer(%22container1%22);%0A
 %09%09%09Strin
@@ -2159,16 +2159,96 @@
       //
+%5Eneeded to create the wav file. no need to download so removed output stream buf
 %0A       
@@ -2286,16 +2286,48 @@
 peech);%0A
+%09%09%09filename = %22gustokoetoname%22;%0A
         
@@ -2336,32 +2336,36 @@
   //
-u
 p
+ay
 load
- to object storage
+s are used to upload 
 %0A   
@@ -2393,22 +2393,26 @@
 adFile(%22
-sample
+container1
 %22, filen

create container for object storage