
package org.apache.juddi.portlets.server.service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.juddi.portlets.client.service.NotifyResponse;
import org.apache.juddi.portlets.client.service.NotifyService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * 
 * @author <a href="mailto:tcunning@apache.org">Tom Cunningham</a>
 *
 */
public class NotifyServiceImpl extends RemoteServiceServlet implements NotifyService {
	
	private static final long serialVersionUID = 6312615857204115941L;
	private Log logger = LogFactory.getLog(this.getClass());
	
	
	public NotifyServiceImpl() {
		super();
	}
	
	public NotifyResponse getSubscriptionNotifications(String authToken) 
	{
		NotifyResponse response = new NotifyResponse();
		try {
			URL url = new URL(getThreadLocalRequest().getScheme(),
							getThreadLocalRequest().getRemoteHost(), 
							getThreadLocalRequest().getLocalPort(), 
							"/juddiv3/notify/");	
			URLConnection con = url.openConnection();
			con.setDoOutput(true);
			con.setDoInput(true);
			InputStream is = con.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			StringBuffer sb = new StringBuffer();
			while (br.ready()) {
				sb.append(br.readLine());
			}
			response.setSubscriptionNotifications(sb.toString());
			response.setSuccess(true);
	     } catch (Exception e) {
	    	 logger.error("Could not obtain token. " + e.getMessage(), e);
	    	 response.setSuccess(false);
	    	 response.setMessage(e.getMessage());
	    	 response.setErrorCode("102");
	     }  catch (Throwable t) {
	    	 logger.error("Could not obtain token. " + t.getMessage(), t);
	    	 response.setSuccess(false);
	    	 response.setMessage(t.getMessage());
	    	 response.setErrorCode("102");
	     } 
		 return response;
	}	
}
