/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.management.rest.resource;

import io.gravitee.management.model.ApiKeyEntity;
import io.gravitee.management.model.ApplicationEntity;
import io.gravitee.management.model.KeysByApplicationEntity;
import io.gravitee.management.service.*;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author David BRASSELY (brasseld at gmail.com)
 */
public class ApiKeysResource extends AbstractResource {

    @Context
    private ResourceContext resourceContext;

    @PathParam("api")
    private String api;

    @Inject
    private ApiService apiService;

    @Inject
    private ApplicationService applicationService;

    @Inject
    private ApiKeyService apiKeyService;

    @Inject
    private PermissionService permissionService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, KeysByApplicationEntity> keys() {
        apiService.findById(this.api);

        permissionService.hasPermission(getAuthenticatedUser(), api, PermissionType.EDIT_API);

        Map<String, List<ApiKeyEntity>> keys = apiKeyService.findByApi(api);
        Map<String, KeysByApplicationEntity> keysByApplication = new HashMap<>(keys.size());

        keys.forEach((application, apiKeyEntities) -> {
            ApplicationEntity applicationEntity = applicationService.findById(application);
            KeysByApplicationEntity keysByApplicationEntity = new KeysByApplicationEntity();

            keysByApplicationEntity.setName(applicationEntity.getName());
            keysByApplicationEntity.setKeys(apiKeyEntities);

            keysByApplication.put(application, keysByApplicationEntity);
        });

        return keysByApplication;
    }

    @DELETE
    @Path("{key}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response revokeApiKey(@PathParam("key") String apiKey) {
        apiService.findById(this.api);

        permissionService.hasPermission(getAuthenticatedUser(), api, PermissionType.EDIT_API);

        apiKeyService.revoke(apiKey);

        return Response
                .status(Response.Status.NO_CONTENT)
                .build();
    }

    @Path("{key}/analytics")
    public ApiKeyAnalyticsResource getApiKeyAnalyticsResource() {
        return resourceContext.getResource(ApiKeyAnalyticsResource.class);
    }
}

@@ -907,16 +907,47 @@
 Inject;%0A
+import javax.validation.Valid;%0A
 import j
@@ -2648,22 +2648,16 @@
 e revoke
-ApiKey
 (@PathPa
@@ -2964,24 +2964,518 @@
 d();%0A    %7D%0A%0A
+    @PUT%0A    @Path(%22%7Bkey%7D%22)%0A    @Consumes(MediaType.APPLICATION_JSON)%0A    @Produces(MediaType.APPLICATION_JSON)%0A    public Response update(@PathParam(%22key%22) String apiKey, @Valid ApiKeyEntity apiKeyEntity) %7B%0A        apiService.findById(this.api);%0A%0A        permissionService.hasPermission(getAuthenticatedUser(), api, PermissionType.EDIT_API);%0A%0A//        apiKeyService.revoke(apiKey);%0A%0A        return Response%0A                .status(Response.Status.NO_CONTENT)%0A                .build();%0A    %7D%0A%0A
     @Path(%22%7B

Update api key for expiration date