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

com.linkedin.restli.internal.client.ResponseImpl Maven / Gradle / Ivy

Go to download

Pegasus is a framework for building robust, scalable service architectures using dynamic discovery and simple asychronous type-checked REST + JSON APIs.

There is a newer version: 6.0.12
Show newest version
/*
   Copyright (c) 2012 LinkedIn Corp.

   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 com.linkedin.restli.internal.client;


import com.linkedin.restli.client.Response;
import com.linkedin.restli.client.RestLiResponseException;
import com.linkedin.restli.client.response.CreateResponse;
import com.linkedin.restli.common.IdResponse;
import com.linkedin.restli.common.ProtocolVersion;
import com.linkedin.restli.common.ComplexResourceKey;
import com.linkedin.restli.common.CompoundKey;
import com.linkedin.restli.common.RestConstants;
import com.linkedin.restli.common.IdEntityResponse;
import com.linkedin.restli.internal.common.ProtocolVersionUtil;
import com.linkedin.restli.internal.common.URIParamUtils;

import java.net.HttpCookie;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import java.util.List;
import java.util.ArrayList;


/**
 * Response for a REST request.
 *
 * @param  response entity template class
 *
 * @author dellamag
 * @author Eran Leshem
 */
public class ResponseImpl implements Response
{
  private int _status = 102;  // SC_PROCESSING
  private final Map _headers;
  private final List _cookies;
  private T _entity;
  private RestLiResponseException _error;

  ResponseImpl(Response origin, RestLiResponseException error)
  {
    this(origin, origin.getEntity());
    _error = error;
  }

  ResponseImpl(int status,
               Map headers,
               List cookies,
               RestLiResponseException error)
  {
    this(status, headers, cookies);
    _error = error;
  }

  public ResponseImpl(Response origin, T entity)
  {
    this(origin.getStatus(), origin.getHeaders(), origin.getCookies());
    _entity = entity;
  }

  public ResponseImpl(int status, Map headers, List cookies, T entity, RestLiResponseException error)
  {
    this(status, headers, cookies);
    _entity = entity;
    _error = error;
  }

  ResponseImpl(int status, Map headers, List cookies)
  {
    _status = status;
    _headers = new TreeMap(String.CASE_INSENSITIVE_ORDER);
    _headers.putAll(headers);
    _cookies = new ArrayList(cookies);
  }

  /**
   * Returns response status.
   */
  @Override public int getStatus()
  {
    return _status;
  }

  /**
   * Returns response body entity.
   */
  @Override public T getEntity()
  {
    return _entity;
  }

  public void setEntity(T entity)
  {
    _entity = entity;
  }

  /**
   * Returns a response header.
   *
   * @param name header name
   */
  @Override public String getHeader(String name)
  {
    return _headers.get(name);
  }

  @Override
  public Map getHeaders()
  {
    return Collections.unmodifiableMap(_headers);
  }

  @Override
  public List getCookies()
  {
    return Collections.unmodifiableList(_cookies);
  }

  /**
   * Specific getter for the 'X-LinkedIn-Id' header
   *
   * @throws UnsupportedOperationException if the entity returned is a {@link CreateResponse} or {@link IdResponse}
   * and the key is a {@link ComplexResourceKey} or {@link CompoundKey}.
   *
   * @deprecated
   * @see {@link com.linkedin.restli.client.Response#getId()}
   */
  @Override
  @Deprecated
  public String getId()
  {
    if (_entity instanceof CreateResponse || _entity instanceof IdResponse || _entity instanceof IdEntityResponse)
    {
      final Object id = checkAndReturnId();
      final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(_headers);
      return URIParamUtils.encodeKeyForHeader(id, protocolVersion);
    }
    else
    {
      return null;
    }
  }

  /**
   * Checks if getId() is supported for this type of {@link Response} and returns the ID if it is.
   *
   * @return The ID if it is supported.
   */
  private Object checkAndReturnId()
  {
    final Object id;
    final String castMessage;

    if (_entity instanceof CreateResponse)
    {
      CreateResponse createResponse = (CreateResponse)_entity;
      id = createResponse.getId();
      castMessage = "CreateResponse";
    }
    else if (_entity instanceof IdEntityResponse)
    {
      IdEntityResponse idEntityResponse = (IdEntityResponse)_entity;
      id = idEntityResponse.getId();
      castMessage = "IdEntityResponse";
    }
    else
    {
      IdResponse idResponse = (IdResponse)_entity;
      id = idResponse.getId();
      castMessage = "IdResponse";
    }
    if (id instanceof CompoundKey || id instanceof ComplexResourceKey)
    {
      String baseErrorMessage = "Cannot call getId() for complex or compound keys! Please cast the object returned by" +
          " getEntity() to a %s and call the getId() method on that.";
      throw new UnsupportedOperationException(String.format(baseErrorMessage, castMessage));
    }
    return id;
  }

  /**
   * Specific getter for the 'Location' header
   */
  @Override
  public URI getLocation()
  {
    return URI.create(getHeader(RestConstants.HEADER_LOCATION));
  }

  @Override
  public RestLiResponseException getError()
  {
    return _error;
  }

  @Override
  public boolean hasError()
  {
    return _error != null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy