/*
 * Copyright © 2015 Cask Data, Inc.
 *
 * 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 co.cask.cdap.gateway.handlers;

import co.cask.cdap.app.services.Data;
import co.cask.cdap.app.store.Store;
import co.cask.cdap.common.conf.Constants;
import co.cask.cdap.data2.datafabric.dataset.service.DatasetInstanceHandler;
import co.cask.cdap.data2.dataset2.DatasetFramework;
import co.cask.cdap.gateway.auth.Authenticator;
import co.cask.cdap.gateway.handlers.util.AbstractAppFabricHttpHandler;
import co.cask.http.HttpResponder;
import com.google.inject.Inject;
import org.jboss.netty.handler.codec.http.HttpRequest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

/**
 *  HttpHandler class for stream and dataset requests in app-fabric.
 */
@Path(Constants.Gateway.API_VERSION_3 + "/namespaces/{namespace-id}")
public class AppFabricDataHttpHandler extends AbstractAppFabricHttpHandler {

  /**
   * Access Dataset Service
   */
  private final DatasetFramework dsFramework;

  /**
   * Store manages non-runtime lifecycle.
   */
  private final Store store;

  /**
   * Constructs an new instance. Parameters are binded by Guice.
   */
  @Inject
  public AppFabricDataHttpHandler(Authenticator authenticator,
                                  Store store, DatasetFramework dsFramework) {
    super(authenticator);
    this.store = store;
    this.dsFramework = dsFramework;
  }

  /**
   * Returns a list of streams in a namespace.
   */
  @GET
  @Path("/streams")
  public void getStreams(HttpRequest request, HttpResponder responder,
                         @PathParam("namespace-id") String namespaceId) {
    dataList(responder, store, dsFramework, Data.STREAM, namespaceId, null, null);
  }

  /**
   * Returns a list of dataset associated with namespace. This is here for the v2 API to use,
   * but was removed in v3 in favor of APIs in {@link DatasetInstanceHandler}.
   */
  void getDatasets(HttpRequest request, HttpResponder responder,
                   @PathParam("namespace-id") String namespaceId) {
    dataList(responder, store, dsFramework, Data.DATASET, namespaceId, null, null);
  }

  /**
   * Returns a dataset associated with namespace. This is here for the v2 API to use,
   * but was removed in v3 in favor of APIs in {@link DatasetInstanceHandler}.
   */
  void getDatasetSpecification(HttpResponder responder,
                               @PathParam("namespace-id") String namespaceId,
                               @PathParam("dataset-id") String datasetId) {
    dataList(responder, store, dsFramework, Data.DATASET, namespaceId, datasetId, null);
  }
}

@@ -657,23 +657,41 @@
 p.ap
-p.services.Data
+i.data.stream.StreamSpecification
 ;%0Aim
@@ -792,62 +792,33 @@
 dap.
-data2.datafabric.dataset.service.DatasetInstanceHandle
+gateway.auth.Authenticato
 r;%0Ai
@@ -840,159 +840,128 @@
 dap.
-data2.dataset2.DatasetFramework;%0Aimport co.cask.cdap.gateway.auth.Authenticator;%0Aimport co.cask.cdap.gateway.handlers.util.AbstractAppFabricHttpHandler
+gateway.handlers.util.AbstractAppFabricHttpHandler;%0Aimport co.cask.cdap.proto.Id;%0Aimport co.cask.cdap.proto.StreamDetail
 ;%0Aim
@@ -989,24 +989,64 @@
 pResponder;%0A
+import com.google.common.collect.Lists;%0A
 import com.g
@@ -1121,16 +1121,130 @@
 equest;%0A
+import org.jboss.netty.handler.codec.http.HttpResponseStatus;%0A%0Aimport java.util.Collection;%0Aimport java.util.List;
 %0Aimport 
@@ -1545,95 +1545,8 @@
  %7B%0A%0A
-  /**%0A   * Access Dataset Service%0A   */%0A  private final DatasetFramework dsFramework;%0A%0A
   /*
@@ -1778,84 +1778,20 @@
 tor,
-%0A                                  Store store, DatasetFramework dsFramework
+ Store store
 ) %7B%0A
@@ -1844,44 +1844,8 @@
 re;%0A
-    this.dsFramework = dsFramework;%0A
   %7D%0A
@@ -2074,424 +2074,283 @@
 pace
-Id
 ) %7B%0A
+%0A
     
-dataList(responder, store, dsFramework, Data.STREAM, namespaceId, null, null);%0A  %7D%0A%0A  /**%0A   * Returns a list of dataset associated with namespace. This is here for the v2 API to use,%0A   * but was removed in v3 in favor of APIs in %7B@link DatasetInstanceHandler%7D.%0A   */%0A  void getDatasets(HttpRequest request, HttpResponder responder,%0A                   @PathParam(%22namespace-id%22) String namespaceId
+Id.Namespace namespaceId = Id.Namespace.from(namespace);%0A    Collection%3CStreamSpecification%3E specs = store.getAllStreams(namespaceId);%0A    List%3CStreamDetail%3E result = Lists.newArrayListWithExpectedSize(specs.size());%0A    for (StreamSpecification spec : specs
 ) %7B%0A    
 data
@@ -2349,378 +2349,214 @@
     
-dataList(responder, store, dsFramework, Data.DATASET, namespaceId, null, null);%0A  %7D%0A%0A  /**%0A   * Returns a dataset associated with namespace. This is here for the v2 API to use,%0A   * but was removed in v3 in favor of APIs in %7B@link DatasetInstanceHa
+  result.add(new StreamDetail(spec.getName()));%0A    %7D%0A    if (result.isEmpty() && store.getNamespace(namespaceId) == null) %7B%0A      respo
 nd
-l
 er
-%7D.%0A   */%0A  void getDatasetSpecification(HttpResponder responder,%0A                               @PathParam(%22n
+.sendString(HttpResponseStatus.NOT_FOUND, String.format(%22N
 amespace
 -id%22
@@ -2547,36 +2547,42 @@
 t(%22Namespace
--id%22) String
+ '%25s' not found.%22,
  namespaceId
@@ -2583,174 +2583,83 @@
 pace
-Id,
+));
 %0A      
-                         @PathParam(%22dataset-id%22) String datasetId) %7B%0A    dataList(responder, store, dsFramework, Data.DATASET, namespaceId, datasetId, null
+return;%0A    %7D%0A    responder.sendJson(HttpResponseStatus.OK, specs
 );%0A 

refactor list .../streams endpoint