2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Iterator;
28 import javax.ws.rs.core.HttpHeaders;
29 import javax.ws.rs.core.MediaType;
31 import org.apache.cassandra.config.ColumnDefinition;
32 import org.apache.cassandra.db.Clustering;
33 import org.apache.cassandra.db.Mutation;
34 import org.apache.cassandra.db.partitions.Partition;
35 import org.apache.cassandra.db.rows.Cell;
36 import org.apache.cassandra.db.rows.Unfiltered;
37 import org.apache.cassandra.db.rows.UnfilteredRowIterator;
38 import org.apache.cassandra.triggers.ITrigger;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.sun.jersey.api.client.Client;
43 import com.sun.jersey.api.client.ClientResponse;
44 import com.sun.jersey.api.client.WebResource;
46 public class MusicTrigger implements ITrigger {
48 private static final Logger logger = LoggerFactory.getLogger(MusicTrigger.class);
50 public Collection<Mutation> augment(Partition partition)
52 String tableName = partition.metadata().cfName;
53 logger.info("MusicTrigger Table: " + tableName);
55 org.json.simple.JSONObject obj = new org.json.simple.JSONObject();
56 obj.put("message_id", partition.metadata().getKeyValidator().getString(partition.partitionKey().getKey()));
59 UnfilteredRowIterator it = partition.unfilteredIterator();
60 while (it.hasNext()) {
61 Unfiltered un = it.next();
62 Clustering clt = (Clustering) un.clustering();
63 Iterator<Cell> cells = partition.getRow(clt).cells().iterator();
64 Iterator<ColumnDefinition> columns = partition.getRow(clt).columns().iterator();
66 while(columns.hasNext()){
67 ColumnDefinition columnDef = columns.next();
68 Cell cell = cells.next();
69 String data = new String(cell.value().array()); // If cell type is text
70 obj.put(columnDef.toString(), data);
73 } catch (Exception e) {
76 logger.info("What is this? "+obj.toString());
78 notifyMusic(obj.toString());
79 return Collections.emptyList();
83 private void notifyMusic(String request) {
84 System.out.println("notifyMusic...");
85 Client client = Client.create();
86 WebResource webResource = client.resource("http://localhost:8080/MUSIC/rest/v2/admin/callbackOps");
88 JSONObject data = new JSONObject();
89 data.setData(request);
91 ClientResponse response = webResource.accept("application/json").type("application/json")
92 .post(ClientResponse.class, data);
94 if(response.getStatus() != 200){
95 System.out.println("Exception...");
97 response.getHeaders().put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
98 response.bufferEntity();
99 String x = response.getEntity(String.class);
100 System.out.println("Response: "+x);
104 /*public Collection<Mutation> augment(Partition partition) {
106 String tableName = partition.metadata().cfName;
107 System.out.println("Table: " + tableName);
109 JSONObject obj = new JSONObject();
110 obj.put("message_id", partition.metadata().getKeyValidator().getString(partition.partitionKey().getKey()));
114 UnfilteredRowIterator it = partition.unfilteredIterator();
115 while (it.hasNext()) {
116 Unfiltered un = it.next();
117 Clustering clt = (Clustering) un.clustering();
118 Iterator<Cell> cls = partition.getRow(clt).cells().iterator();
119 Iterator<ColumnDefinition> columns = partition.getRow(clt).columns().iterator();
121 while(cls.hasNext()){
122 Cell cell = cls.next();
123 String data = new String(cell.value().array()); // If cell type is text
124 System.out.println(cell + " : " +data);
127 while(columns.hasNext()){
128 ColumnDefinition columnDef = columns.next();
129 Cell cell = cls.next();
130 String data = new String(cell.value().array()); // If cell type is text
131 obj.put(columnDef.toString(), data);
134 } catch (Exception e) {
137 System.out.println(obj.toString());
139 return Collections.emptyList();