/*
Copyright (c) 2015 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.server.response;
import com.linkedin.restli.common.HttpStatus;
import com.linkedin.restli.common.RestConstants;
import com.linkedin.restli.internal.server.RoutingResult;
import com.linkedin.restli.internal.server.model.ResourceModel;
import com.linkedin.restli.internal.server.util.AlternativeKeyCoercerException;
import com.linkedin.restli.internal.server.util.ArgumentUtils;
import com.linkedin.restli.server.RestLiServiceException;
/**
* @author mtagle
*/
public class ResponseUtils
{
/**
* If needed, translate a given canonical key to its alternative format.
*
* @param canonicalKey the canonical key
* @param routingResult the routing result
* @return the canonical key if the request did not use or ask for alternative keys, the alternative key otherwise.
*/
static Object translateCanonicalKeyToAlternativeKeyIfNeeded(Object canonicalKey, RoutingResult routingResult)
{
if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM))
{
String altKeyName = routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM);
ResourceModel resourceModel = routingResult.getResourceMethod().getResourceModel();
try
{
return ArgumentUtils.translateToAlternativeKey(canonicalKey, altKeyName, resourceModel);
}
catch (AlternativeKeyCoercerException e)
{
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
String.format("Unexpected Error when coercing canonical key '%s' to alternative key type '%s'", canonicalKey, altKeyName),
e);
}
}
else
{
return canonicalKey;
}
}
}