<commit_before>/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.remoting.simple.transport;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.springframework.remoting.simple.SimpleRemotingException;
import org.springframework.remoting.simple.SimpleRemotingException.Recoverable;
import org.springframework.remoting.simple.protocol.Reply;
import org.springframework.remoting.simple.protocol.Request;

/**
 * @author oliverh
 */
public class HttpTransport extends AbstractTransport {

    private URL serviceUrl;

    public HttpTransport(URL serviceUrl) {
        this.serviceUrl = serviceUrl;

    }

    protected HttpURLConnection openConnection(Request request,
            Authentication authentication) throws IOException {
        URLConnection conn = serviceUrl.openConnection();
        if (!(conn instanceof HttpURLConnection)) {
            throw new UnsupportedOperationException("URL [" + serviceUrl
                    + "] is not a vaild HTTP URL.");
        }
        conn.setDoOutput(true);

        if (authentication != null) {
            String userName = String.valueOf(authentication.getPrincipal());
            String password = String.valueOf(authentication.getCredentials());
            String basicAuth = "Basic " + base64(userName + ":" + password);
            conn.setRequestProperty("Authorization", basicAuth);
        }
        return (HttpURLConnection) conn;
    }

    protected Reply invokeInternal(Request request,
            Authentication authentication, ProgressTracker tracker) {
        try {        
            tracker.start();
            HttpURLConnection conn = doRequest(request, authentication, tracker);
            return doResponse(conn, tracker);
        } finally {
            tracker.finish();
        }
    }

    private HttpURLConnection doRequest(Request request, Authentication authentication, ProgressTracker tracker) {
        OutputStream os = null;
        try {
            tracker.startSending(-1);
            HttpURLConnection conn = openConnection(request, authentication);
            conn.setRequestProperty("Content-Type",
                    "application/x-java-serialized-object");
            os = tracker.getProgressOutputStream(conn.getOutputStream());
            getProtocol().writeRequest(os, request);
            os.flush();
            int responseCode = conn.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                throw new SimpleRemotingException(Recoverable.MAYBE,
                        "Error contecting to server. HTTP response code ["
                                + responseCode + "], response message ["
                                + conn.getResponseMessage() + "].");
            }

            return conn;
        } catch (IOException e) {
            throw new SimpleRemotingException(Recoverable.MAYBE,
                    "Error attempting to connect to server.", e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    logger
                            .warn(
                                    "Ignoring exception when closing output stream.",
                                    e);
                }
            }
            tracker.finishSending();
        }
    }

    private Reply doResponse(URLConnection conn, ProgressTracker tracker) {
        InputStream is = null;
        try {
            tracker.startReceiving(-1);
            is = tracker.getProgressInputStream(conn.getInputStream());
            return getProtocol().readReply(is, Recoverable.MAYBE);
        } catch (IOException e) {
            throw new SimpleRemotingException(Recoverable.MAYBE,
                    "Error reading response from server.", e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    logger.warn(
                            "Ignoring exception when closing input stream.", e);
                }
            }
            tracker.finishReceiving();
        }
    }

    private String base64(String value) {
        StringBuffer cb = new StringBuffer();

        int i = 0;
        for (i = 0; i + 2 < value.length(); i += 3) {
            long chunk = (int) value.charAt(i);
            chunk = (chunk << 8) + (int) value.charAt(i + 1);
            chunk = (chunk << 8) + (int) value.charAt(i + 2);
<commit_msg>credit for BASIC authentication code.<commit_after>/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.remoting.simple.transport;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.springframework.remoting.simple.SimpleRemotingException;
import org.springframework.remoting.simple.SimpleRemotingException.Recoverable;
import org.springframework.remoting.simple.protocol.Reply;
import org.springframework.remoting.simple.protocol.Request;

/**
 * @author oliverh
 */
public class HttpTransport extends AbstractTransport {

    private URL serviceUrl;

    private boolean useAuthenticator;

    public HttpTransport(URL serviceUrl) {
        this.serviceUrl = serviceUrl;
    }

    public void setUseAuthenticator(boolean useAuthenticator) {
        this.useAuthenticator = useAuthenticator;
    }

    protected HttpURLConnection openConnection(Request request,
            Authentication authentication) throws IOException {
        URLConnection conn = serviceUrl.openConnection();
        if (!(conn instanceof HttpURLConnection)) {
            throw new UnsupportedOperationException("URL [" + serviceUrl
                    + "] is not a vaild HTTP URL.");
        }
        conn.setDoOutput(true);
        authenticate(conn, authentication);
        return (HttpURLConnection) conn;
    }

    /*
     * Generate a BASIC authentication header. Borrowed from code in Hessian,
     * thanks to <NAME>.
     */
    protected void authenticate(URLConnection conn,
            Authentication authentication) {
        if (authentication != null) {
            String userName = String.valueOf(authentication.getPrincipal());
            String password = String.valueOf(authentication.getCredentials());
            String basicAuth = "Basic " + base64(userName + ":" + password);
            conn.setRequestProperty("Authorization", basicAuth);
        }
    }

    protected Reply invokeInternal(Request request,
            Authentication authentication, ProgressTracker tracker) {
        try {
            tracker.start();
            HttpURLConnection conn = doRequest(request, authentication, tracker);
            return doResponse(conn, tracker);
        } finally {
            tracker.finish();
        }
    }

    private HttpURLConnection doRequest(Request request,
            Authentication authentication, ProgressTracker tracker) {
        OutputStream os = null;
        try {
            tracker.startSending(-1);
            HttpURLConnection conn = openConnection(request, authentication);
            conn.setRequestProperty("Content-Type",
                    "application/x-java-serialized-object");
            os = tracker.getProgressOutputStream(conn.getOutputStream());
            getProtocol().writeRequest(os, request);
            os.flush();
            int responseCode = conn.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                throw new SimpleRemotingException(Recoverable.MAYBE,
                        "Error contecting to server. HTTP response code ["
                                + responseCode + "], response message ["
                                + conn.getResponseMessage() + "].");
            }

            return conn;
        } catch (IOException e) {
            throw new SimpleRemotingException(Recoverable.MAYBE,
                    "Error attempting to connect to server", e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    logger.warn(
                            "Ignoring exception when closing output stream", e);
                }
            }
            tracker.finishSending();
        }
    }

    private Reply doResponse(URLConnection conn, ProgressTracker tracker) {
        InputStream is = null;
        try {
            tracker.startReceiving(-1);
            is = tracker.getProgressInputStream(conn.getInputStream());
            return getProtocol().readReply(is, Recoverable.MAYBE);
        } catch (IOException e) {
            throw new SimpleRemotingException(Recoverable.MAYBE,
                    "Error reading response from server", e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    logger.warn("Ignoring exception when closing input stream",
                            e);
                }
            }
            tracker.finishReceiving();
        }
    }

    /*
     * Encode a value as base64. Borrowed from code in Hessian, thanks to Scott
     * Ferguson.
     */
    private String base64(String value) {
        StringBuffer cb = new StringBuffer();

        int i = 0;
        for (i = 0; i + 2 < value.length(); i += 3) {
            long chunk = (int) value.charAt(i);
            chunk = (chunk << 8) + (int) value.charAt(i + 1);
            chunk = (chunk << 8) + (int) value.charAt(i + 2);

{'fraction_non_alphanumeric': 0.08987943003288272, 'fraction_numerical': 0.004932407745706979, 'mean_word_length': 2.0422339538760768, 'pattern_counts': {'":': 2, '<': 14, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4047333', 'n_tokens_mistral': 2702, 'n_tokens_neox': 2559, 'n_words': 740}