package stroom.pipeline.server.filter;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;
import stroom.pipeline.server.factory.ConfigurableElement;
import stroom.pipeline.server.factory.ElementIcons;
import stroom.pipeline.server.factory.PipelineProperty;
import stroom.pipeline.shared.data.PipelineElementType;
import stroom.util.logging.StroomLogger;
import stroom.util.spring.StroomScope;

import javax.ws.rs.core.MediaType;

@Component
@Scope(StroomScope.PROTOTYPE)
@ConfigurableElement(type = "HttpPostFilter", category = PipelineElementType.Category.FILTER, roles = {
        PipelineElementType.ROLE_TARGET, PipelineElementType.ROLE_HAS_TARGETS,
        PipelineElementType.VISABILITY_SIMPLE }, icon = ElementIcons.STREAM)
public class HttpPostFilter extends TestFilter {
    private final Client client = Client.create();
    private WebResource webResource;

    private static final StroomLogger LOGGER = StroomLogger.getLogger(HttpPostFilter.class);

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
        String xml = getOutput();
        try {
            ClientResponse response = webResource
                    .accept("application/xml", "application/json")
                    .type(MediaType.APPLICATION_XML)
                    .post(ClientResponse.class, xml);
            LOGGER.info("POSTed document to API.");
        } catch ( Exception e){
            LOGGER.error("Unable to POST document to API.");
        }
    }

    @PipelineProperty(description = "The URL of the receiving API.")
    public void setReceivingApiUrl(final String receivingApiUrl){
        try {
            webResource = client.resource(receivingApiUrl);
        } catch(IllegalArgumentException | NullPointerException e){
            LOGGER.error("Bad URL: {}", receivingApiUrl);
        }
    }

}

@@ -642,16 +642,50 @@
 diaType;
+%0Aimport javax.ws.rs.core.Response;
 %0A%0A@Compo
@@ -947,17 +947,16 @@
 Y_SIMPLE
- 
 %7D, icon 
@@ -1030,96 +1030,8 @@
 er %7B
-%0A    private final Client client = Client.create();%0A    private WebResource webResource;
 %0A%0A  
@@ -1120,16 +1120,104 @@
 .class);
+%0A    private final Client client = Client.create();%0A    private WebResource webResource;
 %0A%0A    @O
@@ -1435,45 +1435,61 @@
 ept(
-%22application/xml%22, %22application/json%22
+MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON
 )%0A  
@@ -1589,24 +1589,250 @@
 lass, xml);%0A
+            if (response.getStatusInfo() != Response.Status.ACCEPTED) %7B%0A                //TODO Add stroom error handling%0A                LOGGER.error(%22POST was not accepted by the API: %7B%7D%22, response);%0A            %7D else %7B%0A    
             
@@ -1863,32 +1863,46 @@
 ment to API.%22);%0A
+            %7D%0A
         %7D catch 
@@ -1902,17 +1902,16 @@
  catch (
- 
 Exceptio
@@ -1906,32 +1906,33 @@
 ch (Exception e)
+ 
 %7B%0A            LO
@@ -1965,34 +1965,40 @@
  document to API
-.%22
+: %7B%7D%22, e
 );%0A        %7D%0A   
@@ -2134,16 +2134,17 @@
 gApiUrl)
+ 
 %7B%0A      
@@ -2226,16 +2226,17 @@
  %7D catch
+ 
 (Illegal
@@ -2278,16 +2278,17 @@
 ption e)
+ 
 %7B%0A      
@@ -2297,29 +2297,131 @@
     
-LOGGER.error(%22Bad URL
+//TODO: Add stroom error handling%0A            LOGGER.error(%22Unable to create an API client with this URL: %7B%7D. Exception was
 : %7B%7D
@@ -2430,32 +2430,35 @@
  receivingApiUrl
+, e
 );%0A        %7D%0A   
@@ -2460,11 +2460,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

Add error handling to HttpPostFilter