/*******************************************************************************
 * Copyright (C) 2016 Black Duck Software, Inc.
 * http://www.blackducksoftware.com/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.blackducksoftware.integration.hub.api.vulnerabilities;

import static com.blackducksoftware.integration.hub.api.UrlConstants.SEGMENT_API;
import static com.blackducksoftware.integration.hub.api.UrlConstants.SEGMENT_COMPONENTS;
import static com.blackducksoftware.integration.hub.api.UrlConstants.SEGMENT_ORIGIN;
import static com.blackducksoftware.integration.hub.api.UrlConstants.SEGMENT_VERSIONS;
import static com.blackducksoftware.integration.hub.api.UrlConstants.SEGMENT_VULNERABILITIES;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.restlet.data.Method;

import com.blackducksoftware.integration.hub.api.HubItemRestService;
import com.blackducksoftware.integration.hub.api.HubRequest;
import com.blackducksoftware.integration.hub.exception.BDRestException;
import com.blackducksoftware.integration.hub.rest.RestConnection;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

public class VulnerabilityRestService extends HubItemRestService<VulnerabilityItem> {
    private static final Type ITEM_TYPE = new TypeToken<VulnerabilityItem>() {
    }.getType();

    private static final Type ITEM_LIST_TYPE = new TypeToken<List<VulnerabilityItem>>() {
    }.getType();

    public VulnerabilityRestService(final RestConnection restConnection, final Gson gson, final JsonParser jsonParser) {
        super(restConnection, gson, jsonParser, ITEM_TYPE, ITEM_LIST_TYPE);
    }

    // TODO need a replacement for this
    @Deprecated
    public List<VulnerabilityItem> getComponentVersionVulnerabilitiesByOrigin(final String componentId,
            final String versionId, final String originId) throws IOException, URISyntaxException, BDRestException {
        final HubRequest itemRequest = new HubRequest(getRestConnection(), getJsonParser());
        itemRequest.setMethod(Method.GET);
        itemRequest.addUrlSegment(SEGMENT_API);
        itemRequest.addUrlSegment(SEGMENT_COMPONENTS);
        itemRequest.addUrlSegment(componentId);
        itemRequest.addUrlSegment(SEGMENT_VERSIONS);
        itemRequest.addUrlSegment(versionId);
        itemRequest.addUrlSegment(SEGMENT_ORIGIN);
        itemRequest.addUrlSegment(originId);
        itemRequest.addUrlSegment(SEGMENT_VULNERABILITIES);

        final JsonObject jsonObject = itemRequest.executeForResponseJson();
        final List<VulnerabilityItem> allItems = getAll(jsonObject, itemRequest);
        return allItems;
    }

    public List<VulnerabilityItem> getComponentVersionVulnerabilities(final String componentVersionUrl)
            throws IOException, URISyntaxException, BDRestException {
        final HubRequest itemRequest = new HubRequest(getRestConnection(), getJsonParser());
        itemRequest.setMethod(Method.GET);
        itemRequest.setUrl(componentVersionUrl);
        itemRequest.addUrlSegment(SEGMENT_VULNERABILITIES);

        final JsonObject jsonObject = itemRequest.executeForResponseJson();
        final List<VulnerabilityItem> allItems = getAll(jsonObject, itemRequest);
        return allItems;
    }

    /**
     * @deprecated Use getComponentVersionVulnerabilities(final String
     *             componentVersionUrl) instead.
     *
     * @param componentId
     * @param versionId
     * @return
     * @throws IOException
     * @throws URISyntaxException
     * @throws BDRestException
     */
    @Deprecated
    public List<VulnerabilityItem> getComponentVersionVulnerabilities(final String componentId, final String versionId)
            throws IOException, URISyntaxException, BDRestException {
        final HubRequest itemRequest = new HubRequest(getRestConnection(), getJsonParser());
        itemRequest.setMethod(Method.GET);
        itemRequest.addUrlSegment(SEGMENT_API);
        itemRequest.addUrlSegment(SEGMENT_COMPONENTS);
        itemRequest.addUrlSegment(componentId);
        itemRequest.addUrlSegment(SEGMENT_VERSIONS);
        itemRequest.addUrlSegment(versionId);
        itemRequest.addUrlSegment(SEGMENT_VULNERABILITIES);

        final JsonObject jsonObject = itemRequest.executeForResponseJson();
        final List<VulnerabilityItem> allItems = getAll(jsonObject, itemRequest);
        return allItems;
    }

    // TODO need a replacement for this
    @Deprecated
    public List<VulnerabilityItem> getComponentVulnerabilities(final String componentId)
            throws IOException, URISyntaxException, BDRestException {
        final HubRequest itemRequest = new HubRequest(getRestConnection(), getJsonParser());
        itemRequest.setMethod(Method.GET);
        itemRequest.addUrlSegment(SEGMENT_API);
        itemRequest.addUrlSegment(SEGMENT_COMPONENTS);
        itemRequest.addUrlSegment(componentId);
        itemRequest.addUrlSegment(SEGMENT_VULNERABILITIES);

        final JsonObject jsonObject = itemRequest.executeForResponseJson();
        final List<VulnerabilityItem> allItems = getAll(jsonObject, itemRequest);
        return allItems;
    }

    // TODO need a replacement for this
    @Deprecated
    public VulnerabilityItem getVulnerability(final String vulnerabilityId)
            throws IOException, URISyntaxException, BDRestException {
        final List<String> segments = new ArrayList<>(3);
        segments.add(SEGMENT_API);
        segments.add(SEGMENT_VULNERABILITIES);
        segments.add(vulnerabilityId);
        return getItem(segments);
    }
}

@@ -3723,32 +3723,31 @@
  String 
-componentVersion
+vulnerabilities
 Url)%0A   
@@ -3980,87 +3980,26 @@
 Url(
-componentVersionUrl);%0A        itemRequest.addUrlSegment(SEGMENT_VULNERABILITIES
+vulnerabilitiesUrl
 );%0A%0A

Change the VulnerabilitiesRestServer to use the url passed