package won.matcher.service.common.service.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

/**
 * Service to use HTTP to request resources
 *
 * User: hfriedrich
 * Date: 04.05.2015
 */
@Component
@Scope("prototype")
public class HttpService
{
  private final Logger log = LoggerFactory.getLogger(getClass());
  private RestTemplate restTemplate;
  private HttpHeaders jsonHeaders;

  public HttpService() {

    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    init(factory);
  }

  public HttpService(int readTimeout, int connectionTimeout) {

    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    factory.setReadTimeout(readTimeout);
    factory.setConnectTimeout(connectionTimeout);
    init(factory);
  }

  private void init(ClientHttpRequestFactory factory) {
    restTemplate = new RestTemplate(factory);
    jsonHeaders = new HttpHeaders();
    jsonHeaders.add("Content-Type", "application/json");
    jsonHeaders.add("Accept", "*/*");
  }

  public void postJsonRequest(String uri, String body) {

    ResponseEntity<String> response = null;
    log.debug("POST URI: {}", uri);
    HttpEntity<String> jsonEntity = new HttpEntity(body, jsonHeaders);
    response = restTemplate.exchange(uri, HttpMethod.POST, jsonEntity, String.class);

    if (response.getStatusCode() != HttpStatus.OK) {
      log.warn("HTTP POST request returned status code: {}", response.getStatusCode());
      throw new HttpClientErrorException(response.getStatusCode());
    }
  }
}

@@ -324,32 +324,102 @@
 RequestFactory;%0A
+import org.springframework.http.converter.StringHttpMessageConverter;%0A
 import org.sprin
@@ -568,16 +568,50 @@
 plate;%0A%0A
+import java.nio.charset.Charset;%0A%0A
 /**%0A * S
@@ -652,16 +652,21 @@
 urces%0A *
+ %3Cp/%3E
 %0A * User
@@ -761,11 +761,13 @@
 vice
-%0A
+ 
 %7B%0A
+  
   pr
@@ -828,16 +828,18 @@
 ass());%0A
+  
   privat
@@ -869,16 +869,18 @@
 late;%0A  
+ 
+ 
 private 
@@ -905,16 +905,18 @@
 aders;%0A%0A
+  
   public
@@ -929,32 +929,36 @@
 ervice() %7B%0A%0A    
+   
+ 
 HttpComponentsCl
@@ -1032,24 +1032,28 @@
 tFactory();%0A
+    
     init(fac
@@ -1059,21 +1059,25 @@
 ctory);%0A
+  
   %7D%0A%0A
+  
   public
@@ -1128,24 +1128,28 @@
 Timeout) %7B%0A%0A
+    
     HttpComp
@@ -1231,24 +1231,28 @@
 tFactory();%0A
+    
     factory.
@@ -1280,24 +1280,28 @@
 meout);%0A    
+   
+ 
 factory.setC
@@ -1330,24 +1330,28 @@
 onTimeout);%0A
+    
     init(fac
@@ -1359,19 +1359,23 @@
 ory);%0A  
+ 
+ 
 %7D%0A%0A
+  
   privat
@@ -1418,24 +1418,28 @@
  factory) %7B%0A
+    
     restTemp
@@ -1468,24 +1468,135 @@
 e(factory);%0A
+        restTemplate.getMessageConverters().add(new StringHttpMessageConverter(Charset.forName(%22UTF-8%22)));%0A    
     jsonHead
@@ -1616,24 +1616,28 @@
 pHeaders();%0A
+    
     jsonHead
@@ -1681,24 +1681,28 @@
 json%22);%0A    
+  
+  
 jsonHeaders.
@@ -1723,21 +1723,25 @@
 %22*/*%22);%0A
+  
   %7D%0A%0A
+  
   public
@@ -1786,24 +1786,28 @@
 ng body) %7B%0A%0A
+    
     Response
@@ -1838,24 +1838,28 @@
 = null;%0A    
+   
+ 
 log.debug(%22P
@@ -1878,16 +1878,20 @@
 , uri);%0A
+    
     Http
@@ -1957,16 +1957,20 @@
 s);%0A    
+ 
+   
 response
@@ -2044,16 +2044,20 @@
 lass);%0A%0A
+    
     if (
@@ -2097,24 +2097,30 @@
 tatus.OK) %7B%0A
+      
       log.wa
@@ -2191,24 +2191,30 @@
 tusCode());%0A
+      
       throw 
@@ -2273,16 +2273,22 @@
 ));%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

use UTF-8 encoding in postJson Http request for Solr matcher