@Override
 public ListResult<T> put(SocializeSession session, String endpoint, Collection<T> objects)
     throws SocializeException {
   endpoint = prepareEndpoint(session, endpoint);
   HttpUriRequest request = requestFactory.getPutRequest(session, endpoint, objects);
   return doListTypeRequest(request, ActionType.UNKNOWN);
 }
 @Override
 public T putAsPost(SocializeSession session, String endpoint, T object)
     throws SocializeException {
   endpoint = prepareEndpoint(session, endpoint);
   HttpUriRequest request = requestFactory.getPostRequest(session, endpoint, object);
   return doGetTypeRequest(request, ActionType.UNKNOWN);
 }
 @Override
 public ListResult<T> list(SocializeSession session, String endpoint, int startIndex, int endIndex)
     throws SocializeException {
   endpoint = prepareEndpoint(session, endpoint);
   HttpUriRequest request = requestFactory.getListRequest(session, endpoint, startIndex, endIndex);
   return doListTypeRequest(request, ActionType.UNKNOWN);
 }
 @Override
 public T get(SocializeSession session, String endpoint, String id, ActionType type)
     throws SocializeException {
   endpoint = prepareEndpoint(session, endpoint);
   HttpUriRequest get = requestFactory.getGetRequest(session, endpoint, id);
   return doGetTypeRequest(get, type);
 }
 @Override
 public ListResult<T> post(
     SocializeSession session, String endpoint, T object, boolean jsonResponse)
     throws SocializeException {
   endpoint = prepareEndpoint(session, endpoint);
   HttpUriRequest request = requestFactory.getPostRequest(session, endpoint, object);
   return doListTypeRequest(request, ActionType.UNKNOWN, jsonResponse);
 }
  @Override
  public void delete(SocializeSession session, String endpoint, String id)
      throws SocializeException {
    HttpEntity entity = null;

    if (!clientFactory.isDestroyed()) {
      try {
        endpoint = prepareEndpoint(session, endpoint);

        HttpClient client = clientFactory.getClient();

        HttpUriRequest del = requestFactory.getDeleteRequest(session, endpoint, id);

        HttpResponse response = client.execute(del);

        entity = response.getEntity();

        if (httpUtils.isHttpError(response)) {

          if (sessionPersister != null && httpUtils.isAuthError(response)) {
            sessionPersister.delete(context);
          }

          String msg = ioUtils.readSafe(entity.getContent());
          throw new SocializeApiError(httpUtils, response.getStatusLine().getStatusCode(), msg);
        }
      } catch (Exception e) {
        throw SocializeException.wrap(e);
      } finally {
        closeEntity(entity);
      }
    } else {
      if (logger != null) {
        logger.warn("Attempt to access HttpClientFactory that was already destroyed");
      }
    }
  }
  @Override
  public SocializeSession authenticate(
      String endpoint, String key, String secret, AuthProviderData data, String uuid)
      throws SocializeException {
    try {
      SessionLock.lock();

      WritableSession session = loadSession(endpoint, key, secret);

      if (session != null) {

        if (validateSession(session, data)) {
          return session;
        } else {
          session = setProviderCredentialsForUser(data, session);
        }
      }

      if (session == null) {
        session = sessionFactory.create(key, secret, data);
      }

      endpoint = prepareEndpoint(session, endpoint, true);

      if (!clientFactory.isDestroyed()) {

        HttpClient client = clientFactory.getClient();

        HttpEntity entity = null;

        try {
          HttpUriRequest request = requestFactory.getAuthRequest(session, endpoint, uuid, data);

          if (logger != null && logger.isDebugEnabled()) {
            logger.debug("Calling authenticate endpoint for device [" + uuid + "]");
          }

          HttpResponse response = executeRequest(client, request);

          entity = response.getEntity();

          if (httpUtils.isHttpError(response)) {

            if (sessionPersister != null && httpUtils.isAuthError(response)) {
              sessionPersister.delete(context);
            }

            String msg = ioUtils.readSafe(entity.getContent());

            throw new SocializeApiError(httpUtils, response.getStatusLine().getStatusCode(), msg);
          } else {

            JSONObject json = jsonParser.parseObject(entity.getContent());

            User user = userFactory.fromJSON(json.getJSONObject("user"));

            String oauth_token = json.getString("oauth_token");
            String oauth_token_secret = json.getString("oauth_token_secret");

            if (StringUtils.isEmpty(oauth_token)) {
              throw new SocializeException("oauth_token was empty in response from server");
            }

            if (StringUtils.isEmpty(oauth_token_secret)) {
              throw new SocializeException("oauth_token_secret was empty in response from server");
            }

            session.setConsumerToken(oauth_token);
            session.setConsumerTokenSecret(oauth_token_secret);
            session.setUser(user);

            setProviderCredentialsForUser(data, session);

            // Ensure the user credentials match the user auth data returned from the server
            verifyProviderCredentialsForUser(session, user);

            saveSession(session);
          }
        } catch (Exception e) {
          throw SocializeException.wrap(e);
        } finally {
          closeEntity(entity);
        }
      } else {
        if (logger != null) {
          logger.warn("Attempt to access HttpClientFactory that was already destroyed");
        }
      }

      return session;
    } finally {
      SessionLock.unlock();
    }
  }