[1/2] ambari git commit: AMBARI-19256 : Asset support Rest API (Belliraj HB via nitirajrathore)

2016-12-23 Thread nitiraj
Repository: ambari
Updated Branches:
  refs/heads/branch-2.5 d1aed2612 -> cf708d65d


http://git-wip-us.apache.org/repos/asf/ambari/blob/cf708d65/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java
--
diff --git 
a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java
 
b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java
new file mode 100644
index 000..0622971
--- /dev/null
+++ 
b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java
@@ -0,0 +1,197 @@
+/**
+ * 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 org.apache.oozie.ambari.view.assets;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+import org.apache.ambari.view.ViewContext;
+import org.apache.oozie.ambari.view.*;
+import org.apache.oozie.ambari.view.assets.model.ActionAsset;
+import org.apache.oozie.ambari.view.assets.model.ActionAssetDefinition;
+import org.apache.oozie.ambari.view.assets.model.AssetDefintion;
+import org.apache.oozie.ambari.view.model.APIResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.*;
+import javax.ws.rs.core.*;
+import javax.ws.rs.core.Response.Status;
+import java.io.IOException;
+import java.util.*;
+
+import static org.apache.oozie.ambari.view.Constants.*;
+
+public class AssetResource {
+
+  private final static Logger LOGGER = LoggerFactory
+.getLogger(AssetResource.class);
+  private final AssetService assetService;
+  private final ViewContext viewContext;
+  private final HDFSFileUtils hdfsFileUtils;
+  private OozieUtils oozieUtils = new OozieUtils();
+  private final OozieDelegate oozieDelegate;
+
+
+  public AssetResource(ViewContext viewContext) {
+this.viewContext = viewContext;
+this.assetService = new AssetService(viewContext);
+hdfsFileUtils = new HDFSFileUtils(viewContext);
+oozieDelegate = new OozieDelegate(viewContext);
+  }
+
+  @GET
+  public Response getAssets() {
+try {
+  Collection assets = assetService.getAssets();
+  APIResult result = new APIResult();
+  result.setStatus(APIResult.Status.SUCCESS);
+  result.getPaging().setTotal(assets != null ? assets.size() : 0L);
+  result.setData(assets);
+  return Response.ok(result).build();
+} catch (Exception e) {
+  throw new ServiceFormattedException(e);
+}
+  }
+
+  @GET
+  @Path("/mine")
+  public Response getMyAssets() {
+try {
+  Collection assets = assetService.getMyAssets();
+  APIResult result = new APIResult();
+  result.setStatus(APIResult.Status.SUCCESS);
+  result.getPaging().setTotal(assets != null ? assets.size() : 0L);
+  result.setData(assets);
+  return Response.ok(result).build();
+} catch (Exception e) {
+  throw new ServiceFormattedException(e);
+}
+  }
+  @POST
+  public Response saveAsset(@Context HttpHeaders headers,
+@QueryParam("id") String id, @Context UriInfo ui, 
String body) {
+try {
+  Gson gson = new Gson();
+  AssetDefintion assetDefinition = gson.fromJson(body,
+AssetDefintion.class);
+  Map validateAsset = validateAsset(headers,
+assetDefinition.getDefinition(), ui.getQueryParameters());
+  if (!STATUS_OK.equals(validateAsset.get(STATUS_KEY))) {
+return Response.status(Status.BAD_REQUEST).build();
+  }
+  assetService.saveAsset(id, viewContext.getUsername(), assetDefinition);
+  APIResult result = new APIResult();
+  result.setStatus(APIResult.Status.SUCCESS);
+  return Response.ok(result).build();
+} catch (Exception e) {
+  throw new ServiceFormattedException(e);
+}
+  }
+
+  private List getAsList(String string) {
+ArrayList li = new ArrayList<>(1);
+li.add(string);
+return li;
+  }
+
+  public Map validateAsset(HttpHeaders headers,
+   String postBody, 
MultivaluedMap queryParams) {
+String workflowXml = oozieUtils.generateWorkflowXml(postBody);
+try {
+  Map result = ne

[1/2] ambari git commit: AMBARI-19256 : Asset support Rest API (Belliraj HB via nitirajrathore)

2016-12-23 Thread nitiraj
Repository: ambari
Updated Branches:
  refs/heads/trunk e90009310 -> 47a988245


http://git-wip-us.apache.org/repos/asf/ambari/blob/47a98824/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java
--
diff --git 
a/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java
 
b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java
new file mode 100644
index 000..0622971
--- /dev/null
+++ 
b/contrib/views/wfmanager/src/main/java/org/apache/oozie/ambari/view/assets/AssetResource.java
@@ -0,0 +1,197 @@
+/**
+ * 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 org.apache.oozie.ambari.view.assets;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+import org.apache.ambari.view.ViewContext;
+import org.apache.oozie.ambari.view.*;
+import org.apache.oozie.ambari.view.assets.model.ActionAsset;
+import org.apache.oozie.ambari.view.assets.model.ActionAssetDefinition;
+import org.apache.oozie.ambari.view.assets.model.AssetDefintion;
+import org.apache.oozie.ambari.view.model.APIResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.*;
+import javax.ws.rs.core.*;
+import javax.ws.rs.core.Response.Status;
+import java.io.IOException;
+import java.util.*;
+
+import static org.apache.oozie.ambari.view.Constants.*;
+
+public class AssetResource {
+
+  private final static Logger LOGGER = LoggerFactory
+.getLogger(AssetResource.class);
+  private final AssetService assetService;
+  private final ViewContext viewContext;
+  private final HDFSFileUtils hdfsFileUtils;
+  private OozieUtils oozieUtils = new OozieUtils();
+  private final OozieDelegate oozieDelegate;
+
+
+  public AssetResource(ViewContext viewContext) {
+this.viewContext = viewContext;
+this.assetService = new AssetService(viewContext);
+hdfsFileUtils = new HDFSFileUtils(viewContext);
+oozieDelegate = new OozieDelegate(viewContext);
+  }
+
+  @GET
+  public Response getAssets() {
+try {
+  Collection assets = assetService.getAssets();
+  APIResult result = new APIResult();
+  result.setStatus(APIResult.Status.SUCCESS);
+  result.getPaging().setTotal(assets != null ? assets.size() : 0L);
+  result.setData(assets);
+  return Response.ok(result).build();
+} catch (Exception e) {
+  throw new ServiceFormattedException(e);
+}
+  }
+
+  @GET
+  @Path("/mine")
+  public Response getMyAssets() {
+try {
+  Collection assets = assetService.getMyAssets();
+  APIResult result = new APIResult();
+  result.setStatus(APIResult.Status.SUCCESS);
+  result.getPaging().setTotal(assets != null ? assets.size() : 0L);
+  result.setData(assets);
+  return Response.ok(result).build();
+} catch (Exception e) {
+  throw new ServiceFormattedException(e);
+}
+  }
+  @POST
+  public Response saveAsset(@Context HttpHeaders headers,
+@QueryParam("id") String id, @Context UriInfo ui, 
String body) {
+try {
+  Gson gson = new Gson();
+  AssetDefintion assetDefinition = gson.fromJson(body,
+AssetDefintion.class);
+  Map validateAsset = validateAsset(headers,
+assetDefinition.getDefinition(), ui.getQueryParameters());
+  if (!STATUS_OK.equals(validateAsset.get(STATUS_KEY))) {
+return Response.status(Status.BAD_REQUEST).build();
+  }
+  assetService.saveAsset(id, viewContext.getUsername(), assetDefinition);
+  APIResult result = new APIResult();
+  result.setStatus(APIResult.Status.SUCCESS);
+  return Response.ok(result).build();
+} catch (Exception e) {
+  throw new ServiceFormattedException(e);
+}
+  }
+
+  private List getAsList(String string) {
+ArrayList li = new ArrayList<>(1);
+li.add(string);
+return li;
+  }
+
+  public Map validateAsset(HttpHeaders headers,
+   String postBody, 
MultivaluedMap queryParams) {
+String workflowXml = oozieUtils.generateWorkflowXml(postBody);
+try {
+  Map result = new Has