All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iwillfailyou.inspection.badge.IwfyBadge Maven / Gradle / Ivy

There is a newer version: 0.8.1
Show newest version
package com.iwillfailyou.inspection.badge;

import com.iwillfailyou.inspection.InspectionException;
import com.iwillfailyou.inspection.Violation;
import com.iwillfailyou.inspection.Violations;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public final class IwfyBadge implements Badge {

    private final Violations violations;
    private final int threshold;

    public IwfyBadge(final Violations violations) {
        this(violations, 0);
    }

    public IwfyBadge(
        final Violations violations,
        final int threshold
    ) {
        this.violations = violations;
        this.threshold = threshold;
    }

    @Override
    public void send(final URL url) throws InspectionException {
        try (
            final CloseableHttpClient httpClient = HttpClients.createDefault()
        ) {
            final List form = new ArrayList<>();
            for (final Violation violation : violations.asList()) {
                form.add(
                    new BasicNameValuePair(
                        "violation",
                        violation.description()
                    )
                );
            }
            form.add(
                new BasicNameValuePair(
                    "threshold",
                    String.valueOf(threshold)
                )
            );
            final HttpPost saveBadgeInfo = new HttpPost(url.toURI());
            saveBadgeInfo.setEntity(
                new UrlEncodedFormEntity(
                    form
                )
            );
            httpClient.execute(saveBadgeInfo).close();
        } catch (final ClientProtocolException e) {
            throw new InspectionException(
                "Static service error when sending badge info.",
                e
            );
        } catch (final URISyntaxException | IOException e) {
            throw new InspectionException("Can not send the badge request.", e);
        }
    }

    @Override
    public void failIfRed() throws InspectionException {
        final List violations = this.violations.asList();
        if (violations.size() > threshold) {
            final StringBuilder message = new StringBuilder();
            for (final Violation violation : violations) {
                message.append("Found:\n");
                message.append(violation.description());
                message.append('\n');
            }
            throw new InspectionException(
                String.format(
                    "Found statics:\n%s",
                    message.toString()
                )
            );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy