<commit_before>package com.hbakkum.rundeck.plugins.hipchat.roomnotifier;

import com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginException;
import com.hbakkum.rundeck.plugins.hipchat.http.HttpRequestExecutor;
import com.hbakkum.rundeck.plugins.hipchat.http.HttpResponse;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;

import java.io.IOException;

import static com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginUtils.urlEncode;

/**
 * @author <NAME>
 */
public class HipChatApiVersion2RoomNotifier implements HipChatRoomNotifier {

    private static final String HIPCHAT_API_ROOM_NOTIFICATION_URL_PATH = "room/%s/notification";
    private static final String HIPCHAT_API_ROOM_NOTIFICATION_URL_QUERY = "?auth_token=%s";
    private static final String HIPCHAT_API_VERSION = "v2";

    private final HttpRequestExecutor httpRequestExecutor;

    public HipChatApiVersion2RoomNotifier(final HttpRequestExecutor httpRequestExecutor) {
        this.httpRequestExecutor = httpRequestExecutor;
    }

    @Override
    public void sendRoomNotification(
            final String baseURL,
            final String room,
            final String message,
            final String color,
            final String authToken) {

        final ObjectNode requestBody = JsonNodeFactory.instance.objectNode();
        requestBody.put("message", message);
        requestBody.put("color", color);
        requestBody.put("message_format", "html");

        final String urlPath = String.format(HIPCHAT_API_ROOM_NOTIFICATION_URL_PATH, urlEncode(room));
        final String urlQueryString = String.format(HIPCHAT_API_ROOM_NOTIFICATION_URL_QUERY, urlEncode(authToken));

        final HttpResponse httpResponse = httpRequestExecutor.execute(baseURL + "/" + HIPCHAT_API_VERSION + "/" + urlPath + urlQueryString, requestBody.toString());

        if (httpResponse.getResponseCode() != HttpResponse.STATUS__NO_CONTENT) {
            throw toHipChatNotificationPluginException(httpResponse);
        }
    }

    @Override
    public String getSupportedApiVersion() {
        return HIPCHAT_API_VERSION;
    }

    private HipChatNotificationPluginException toHipChatNotificationPluginException(final HttpResponse httpResponse) {
        if (HttpResponse.CONTENT_TYPE__JSON.equals(httpResponse.getContentType())) {
            final String errorMessage = getErrorMessage(httpResponse.getResponseBody());
            if (errorMessage != null && !errorMessage.isEmpty()) {
                return new HipChatNotificationPluginException("HipChat API returned an error: ["+errorMessage+"]");
            }
        }

        return new HipChatNotificationPluginException("Unexpected response received from HipChat API: ["+httpResponse+"]");
    }<commit_msg>Issue 11 : Notify room<commit_after>package com.hbakkum.rundeck.plugins.hipchat.roomnotifier;

import com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginException;
import com.hbakkum.rundeck.plugins.hipchat.http.HttpRequestExecutor;
import com.hbakkum.rundeck.plugins.hipchat.http.HttpResponse;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;

import java.io.IOException;

import static com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginUtils.urlEncode;

/**
 * @author <NAME>
 */
public class HipChatApiVersion2RoomNotifier implements HipChatRoomNotifier {

    private static final String HIPCHAT_API_ROOM_NOTIFICATION_URL_PATH = "room/%s/notification";
    private static final String HIPCHAT_API_ROOM_NOTIFICATION_URL_QUERY = "?auth_token=%s";
    private static final String HIPCHAT_API_VERSION = "v2";

    private final HttpRequestExecutor httpRequestExecutor;

    public HipChatApiVersion2RoomNotifier(final HttpRequestExecutor httpRequestExecutor) {
        this.httpRequestExecutor = httpRequestExecutor;
    }

    @Override
    public void sendRoomNotification(
            final String baseURL,
            final String room,
            final String message,
            final String color,
            final String authToken) {

        final ObjectNode requestBody = JsonNodeFactory.instance.objectNode();
        requestBody.put("message", message);
        requestBody.put("color", color);
        requestBody.put("message_format", "html");
        requestBody.put("notify", "true");


        final String urlPath = String.format(HIPCHAT_API_ROOM_NOTIFICATION_URL_PATH, urlEncode(room));
        final String urlQueryString = String.format(HIPCHAT_API_ROOM_NOTIFICATION_URL_QUERY, urlEncode(authToken));

        final HttpResponse httpResponse = httpRequestExecutor.execute(baseURL + "/" + HIPCHAT_API_VERSION + "/" + urlPath + urlQueryString, requestBody.toString());

        if (httpResponse.getResponseCode() != HttpResponse.STATUS__NO_CONTENT) {
            throw toHipChatNotificationPluginException(httpResponse);
        }
    }

    @Override
    public String getSupportedApiVersion() {
        return HIPCHAT_API_VERSION;
    }

    private HipChatNotificationPluginException toHipChatNotificationPluginException(final HttpResponse httpResponse) {
        if (HttpResponse.CONTENT_TYPE__JSON.equals(httpResponse.getContentType())) {
            final String errorMessage = getErrorMessage(httpResponse.getResponseBody());
            if (errorMessage != null && !errorMessage.isEmpty()) {
                return new HipChatNotificationPluginException("HipChat API returned an error: ["+errorMessage+"]");
            }
        }

        return new HipChatNotificationPluginException("Unexpected response received from HipChat API: ["+httpResponse+"]");
    }
{'fraction_non_alphanumeric': 0.08546117769671704, 'fraction_numerical': 0.001389612645475074, 'mean_word_length': 4.118222222222222, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1094937', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1534, 'n_words': 293}