package com.atlassian.stash.internal.rest.util;

import com.sun.jersey.api.core.HttpContext;

import javax.ws.rs.core.PathSegment;
import java.util.List;

public class HttpContextUtils {

    private HttpContextUtils() {
        throw new UnsupportedOperationException(getClass().getName() +
                " is a utility class and should not be instantiated");
    }

    public static String requireSinglePathSegmentValue(String name, HttpContext httpContext, Class desiredClass) {
        List<PathSegment> pathSegments = httpContext.getUriInfo().getPathSegments(name);
        if (pathSegments.size() != 1) {
            throw new IllegalStateException(String.format("There must be one (and only one) {%s} path parameter " +
                    "defined in the resource's path to support injecting a %s via the @Context attribute.", name,
                    desiredClass.getSimpleName()));
        }
        return pathSegments.get(0).getPath();
    }
}
