<commit_before>/**
 * Copyright (C) 2009 - 2009 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.transport.jaxrs;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicHeader;
import org.fudgemsg.FudgeContext;
import org.fudgemsg.FudgeField;
import org.fudgemsg.FudgeFieldContainer;
import org.fudgemsg.FudgeMsgEnvelope;
import org.fudgemsg.FudgeMsgWriter;
import org.fudgemsg.mapping.FudgeDeserializationContext;
import org.fudgemsg.mapping.FudgeSerializationContext;

import com.opengamma.OpenGammaRuntimeException;

/**
 * HttpClient wrapper with some additional helper functions for Fudge based JAX-RS calls.
 */
public class RestClient {

  private static final String FUDGE_MIME_TYPE = "application/vnd.fudgemsg";
  private static final Header s_accept = new BasicHeader("Accept", FUDGE_MIME_TYPE);
  private static final Header s_contentType = new BasicHeader("Content-Type", FUDGE_MIME_TYPE);

  private final FudgeContext _fudgeContext;
  private final HttpClient _httpClient;

  protected RestClient(final FudgeContext fudgeContext, final HttpClient underlyingClient) {
    _fudgeContext = fudgeContext;
    _httpClient = underlyingClient;
  }

  /**
   * Create a default instance with whatever security credentials/parameters etc... are relevant
   * to the user's context. This is here as a placeholder - passing NULL will give a default and
   * anonymous connection.
   * @param fudgeContext  the context, not null
   * @param securityCredentials  the security details, not null
   * @return the RESTful client, not null
   */
  public static RestClient getInstance(FudgeContext fudgeContext, Map<String, String> securityCredentials) {
    return new RestClient(fudgeContext, new DefaultHttpClient());<commit_msg>Adding Stephen's code to make RestClient thread-safe<commit_after>/**
 * Copyright (C) 2009 - 2009 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.transport.jaxrs;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ClientConnectionManagerFactory;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpParams;
import org.fudgemsg.FudgeContext;
import org.fudgemsg.FudgeField;
import org.fudgemsg.FudgeFieldContainer;
import org.fudgemsg.FudgeMsgEnvelope;
import org.fudgemsg.FudgeMsgWriter;
import org.fudgemsg.mapping.FudgeDeserializationContext;
import org.fudgemsg.mapping.FudgeSerializationContext;

import com.opengamma.OpenGammaRuntimeException;

/**
 * HttpClient wrapper with some additional helper functions for Fudge based JAX-RS calls.
 */
public class RestClient {

  private static final String FUDGE_MIME_TYPE = "application/vnd.fudgemsg";
  private static final Header s_accept = new BasicHeader("Accept", FUDGE_MIME_TYPE);
  private static final Header s_contentType = new BasicHeader("Content-Type", FUDGE_MIME_TYPE);

  private final FudgeContext _fudgeContext;
  private final HttpClient _httpClient;

  protected RestClient(final FudgeContext fudgeContext, final HttpClient underlyingClient) {
    _fudgeContext = fudgeContext;
    _httpClient = underlyingClient;
  }

  /**
   * Create a default instance with whatever security credentials/parameters etc... are relevant
   * to the user's context. This is here as a placeholder - passing NULL will give a default and
   * anonymous connection.
   * @param fudgeContext  the context, not null
   * @param securityCredentials  the security details, not null
   * @return the RESTful client, not null
   */
  public static RestClient getInstance(FudgeContext fudgeContext, Map<String, String> securityCredentials) {
    DefaultHttpClient dummyClient = new DefaultHttpClient();
    HttpParams params = dummyClient.getParams();
    params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, TSCMFactory.class.getName());

    return new RestClient(fudgeContext, new DefaultHttpClient(params));
  }
  
  /**
   * Factory for creating a thread-safe connection manager.
   */
  public static class TSCMFactory implements ClientConnectionManagerFactory {
    @Override
    public ClientConnectionManager newInstance(HttpParams params, SchemeRegistry schemeRegistry) {
      return new ThreadSafeClientConnManager(params, schemeRegistry);
    }
{'fraction_non_alphanumeric': 0.08383751080380294, 'fraction_numerical': 0.002765773552290406, 'mean_word_length': 5.393370165745856, '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': '4933033', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1635, 'n_words': 416}