/**
 * Copyright (C) 2009-2013 Nasrollah Kavian - All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/
 */
package io.konverge.library.utility.network;

import io.konverge.library.utility.common.Utility;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

@SuppressWarnings("javadoc")
public final class HTTPPost extends HTTPBase
   {
   private boolean                        m_enableRandom = true;
   private final List<BasicNameValuePair> m_parameters   = new ArrayList<BasicNameValuePair>();

   public HTTPPost(final String address)
      {
      super(address);
      }

   public HTTPPost(final String address,final OutputStream outputStream)
      {
      super(address);
      m_outputStream = outputStream;
      }

   public HTTPPost execute()
      {
      if(m_enableRandom)
         {
         setParameter("_random_",String.valueOf(s_random.nextLong()));
         }
      try
         {
         final DefaultHttpClient client = new DefaultHttpClient();
         if(s_hasProxy)
            {
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,s_hostProxy);
            }
         if(s_cookieStore != null)
            {
            client.setCookieStore(s_cookieStore);
            }
         final HttpPost httpost = new HttpPost(m_address);
         httpost.setHeader(org.apache.http.protocol.HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded");
         httpost.setEntity(new UrlEncodedFormEntity(m_parameters,org.apache.http.protocol.HTTP.UTF_8));
         final HttpResponse response = client.execute(httpost);
         s_cookieStore = client.getCookieStore();
         m_statusCode = response.getStatusLine().getStatusCode();
         if(m_outputStream != null)
            {
            Utility.pipe(response.getEntity().getContent(),m_outputStream);
            m_outputStream.close();
            }
         else
            {
            m_response = EntityUtils.toString(response.getEntity());
            //System.out.println("----------- " + m_address + "\n\n----------- " + m_response);
            }
         }
      catch(final Exception e)
         {
         //Konverge.capture(Configuration.getContext(),e,false);
         }
      return this;
      }

   public HTTPPost setEnableRandom(final boolean enableRandom)
      {
      m_enableRandom = enableRandom;
      return this;
      }

   public HTTPPost setParameter(final String name,final long value)
      {
      setParameter(name,String.valueOf(value));
      return this;
      }

   public HTTPPost setParameter(final String name,final String value)
      {
      m_parameters.add(new BasicNameValuePair(name,value));
      return this;
      }
   }
