Package com.linkedin.restli.internal.client

Source Code of com.linkedin.restli.internal.client.ResponseImpl

/*
   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.internal.common.ProtocolVersionUtil;
import com.linkedin.restli.internal.common.URIParamUtils;

import java.net.URI;
import java.util.Collections;
import java.util.Map;


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

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

  ResponseImpl(int status, Map<String, String> headers, RestLiResponseException error)
  {
    this(status, headers);
    _error = error;
  }

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

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

  ResponseImpl(int status, Map<String, String> headers)
  {
    _status = status;
    _headers = headers;
  }

  /**
   * 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<String, String> getHeaders()
  {
    return Collections.unmodifiableMap(_headers);
  }

  /**
   * 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 != null && (_entity instanceof CreateResponse<?> || _entity instanceof IdResponse<?>))
    {
      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
    {
      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;
  }
}
TOP

Related Classes of com.linkedin.restli.internal.client.ResponseImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.