package de.tblsoft.solr.pipeline.filter;

import com.google.common.base.Strings;
import de.tblsoft.solr.http.HTTPHelper;
import de.tblsoft.solr.pipeline.AbstractFilter;
import de.tblsoft.solr.pipeline.bean.Document;

/**
 * Created by tblsoft 17.05.16.
 */
public class LinkCheckerFilter extends AbstractFilter {

    private String urlFieldName;
    private String httpCodeFieldName;
    private String statusFieldName;


    @Override
    public void init() {
        urlFieldName = getProperty("urlFieldName", "url");
        httpCodeFieldName = getProperty("httpCodeFieldName", "httpCode");
        statusFieldName = getProperty("statusFieldName", "status");
        super.init();
    }

    @Override
    public void document(Document document) {
        String httpCode;
        String status;
        String url = document.getFieldValue(urlFieldName);
        if(Strings.isNullOrEmpty(url)) {
            httpCode = "0";
            status = "NOURL";
        } else {
            int httpStatusCode = HTTPHelper.getStatusCode(url);
            httpCode = String.valueOf(httpStatusCode);
            status = "ERROR";
            if(httpStatusCode == 200) {
                status = "OK";
            }
        }



        document.addField(httpCodeFieldName, httpCode);
        document.addField(statusFieldName, status);

        super.document(document);
    }
}

@@ -1113,13 +1113,15 @@
  = %22
-ERROR
+UNKNOWN
 %22;%0A 
@@ -1153,74 +1153,686 @@
 ode 
-== 200) %7B%0A                status = %22OK%22;%0A            %7D%0A        %7D%0A%0A
+%3E= 100 && httpStatusCode %3C 200) %7B%0A                status = %22INFORMATION%22;%0A            %7D%0A            if(httpStatusCode %3E= 200 && httpStatusCode %3C 300) %7B%0A                status = %22OK%22;%0A            %7D%0A            if(httpStatusCode %3E= 300 && httpStatusCode %3C 400) %7B%0A                status = %22REDIRECT%22;%0A            %7D%0A            if(httpStatusCode %3E= 400 && httpStatusCode %3C 500) %7B%0A                status = %22CLIENT_ERROR%22;%0A            %7D%0A            if(httpStatusCode %3E= 500 && httpStatusCode %3C 600) %7B%0A                status = %22SERVER_ERROR%22;%0A            %7D%0A            if(httpStatusCode %3E= 900 && httpStatusCode %3C 1000) %7B%0A                status = %22PROPRIETARY%22;%0A            %7D%0A        %7D
 %0A%0A  

improve the link checker filter