/*
 * Copyright 2013 SURFnet bv, The Netherlands
 *
 * 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 selfservice.sab;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;

import static java.lang.String.format;
import static java.net.URLEncoder.encode;

@Component
public class HttpClientTransport implements SabTransport {

  private static final Integer TIMEOUT = new Integer(10000);
  private HttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());

  private final UsernamePasswordCredentials samlCredentials;
  private final UsernamePasswordCredentials restCredentials;
  private final URI sabEndpoint;
  private final URI restEndPoint;

  @Autowired
  public HttpClientTransport(@Value("sab.username") String sabUserName,
                             @Value("sab.password") String sabPassword,
                             @Value("sab-rest.username") String sabRestUserName,
                             @Value("sab-rest.password") String sabRestPassword,
                             @Value("${sab.endpoint}") URI sabEndpoint,
                             @Value("${sab-rest.endpoint}") URI restEndPoint) {
    this.samlCredentials = new UsernamePasswordCredentials(sabUserName, sabPassword);
    this.restCredentials = new UsernamePasswordCredentials(sabRestUserName, sabRestPassword);
    this.sabEndpoint = sabEndpoint;
    this.restEndPoint = restEndPoint;
    httpClient.getParams().setParameter("http.socket.timeout", TIMEOUT);
  }


  @Override
  public InputStream getResponse(final String request) throws IOException {
    HttpPost httpPost = new HttpPost(sabEndpoint);
    StringEntity stringEntity = new StringEntity(request);
    httpPost.setEntity(stringEntity);
    return handleRequest(httpPost, samlCredentials);
  }

  @Override
  public InputStream getRestResponse(String organisationAbbreviation, String role) {
    HttpGet httpGet = null;
    try {
      httpGet = new HttpGet(format("%s/profile?abbrev=%s&role=%s", restEndPoint, encode(organisationAbbreviation, "UTF-8"), encode(role, "UTF-8")));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
    return handleRequest(httpGet, restCredentials);
  }

  private InputStream handleRequest(HttpRequestBase request, UsernamePasswordCredentials credentials) {
    try {
      request.addHeader("Authorization", "Basic " + encodeUserPass(credentials));
      HttpResponse httpResponse = httpClient.execute(request);
      return httpResponse.getEntity().getContent();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  private String encodeUserPass(UsernamePasswordCredentials credentials) {
    return new String(Base64.encodeBase64(format("%s:%s", credentials.getUserName(), credentials.getPassword()).getBytes()));
  }

}

@@ -627,16 +627,221 @@
 e.sab;%0A%0A
+import static java.lang.String.format;%0Aimport static java.net.URLEncoder.encode;%0A%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.URI;%0A%0A
 import o
@@ -879,16 +879,16 @@
 Base64;%0A
-
 import o
@@ -1504,213 +1504,8 @@
 t;%0A%0A
-import java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.URI;%0A%0Aimport static java.lang.String.format;%0Aimport static java.net.URLEncoder.encode;%0A%0A
 @Com
@@ -1642,16 +1642,22 @@
 private 
+final 
 HttpClie
@@ -1970,24 +1970,26 @@
 ort(@Value(%22
+$%7B
 sab.username
@@ -1980,32 +1980,33 @@
 (%22$%7Bsab.username
+%7D
 %22) String sabUse
@@ -2049,16 +2049,18 @@
 @Value(%22
+$%7B
 sab.pass
@@ -2055,32 +2055,33 @@
 (%22$%7Bsab.password
+%7D
 %22) String sabPas
@@ -2116,32 +2116,34 @@
         @Value(%22
+$%7B
 sab-rest.usernam
@@ -2143,16 +2143,17 @@
 username
+%7D
 %22) Strin
@@ -2208,16 +2208,18 @@
 @Value(%22
+$%7B
 sab-rest
@@ -2227,16 +2227,17 @@
 password
+%7D
 %22) Strin
@@ -2738,14 +2738,13 @@
 T);%0A
+
   %7D%0A%0A
-%0A
   @O

Properly inject sab username and passwords