|
17 | 17 | * under the License.
|
18 | 18 | */
|
19 | 19 |
|
20 |
| -package org.apache.druid.server.http; |
| 20 | +package org.apache.druid.server.extension.service; |
21 | 21 |
|
22 |
| -import com.fasterxml.jackson.annotation.JsonInclude; |
23 |
| -import com.fasterxml.jackson.annotation.JsonProperty; |
24 |
| -import com.fasterxml.jackson.core.type.TypeReference; |
25 | 22 | import com.fasterxml.jackson.databind.ObjectMapper;
|
26 |
| -import com.fasterxml.jackson.jaxrs.smile.SmileMediaTypes; |
27 |
| -import com.google.common.annotations.VisibleForTesting; |
28 |
| -import com.google.common.base.Strings; |
29 | 23 | import com.google.common.base.Supplier;
|
30 |
| -import com.google.common.net.HostAndPort; |
31 | 24 | import com.google.inject.Inject;
|
32 |
| -import com.sun.jersey.spi.container.ResourceFilters; |
33 |
| -import org.apache.druid.audit.AuditManager; |
| 25 | +import org.apache.druid.guice.ManageLifecycle; |
34 | 26 | import org.apache.druid.guice.annotations.Json;
|
35 |
| -import org.apache.druid.guice.annotations.Smile; |
36 |
| -import org.apache.druid.java.util.common.IAE; |
37 |
| -import org.apache.druid.java.util.common.RE; |
38 |
| -import org.apache.druid.java.util.common.logger.Logger; |
| 27 | +import org.apache.druid.java.util.common.StringUtils; |
| 28 | +import org.apache.druid.java.util.common.lifecycle.LifecycleStart; |
| 29 | +import org.apache.druid.java.util.common.lifecycle.LifecycleStop; |
39 | 30 | import org.apache.druid.java.util.emitter.service.ServiceEmitter;
|
40 | 31 | import org.apache.druid.metadata.MetadataStorageTablesConfig;
|
41 | 32 | import org.apache.druid.metadata.SQLMetadataConnector;
|
42 |
| -import org.apache.druid.query.lookup.LookupsState; |
43 |
| -import org.apache.druid.server.audit.AuditManagerConfig; |
44 |
| -import org.apache.druid.server.audit.AuditSerdeHelper; |
45 |
| -import org.apache.druid.server.extension.service.TestDbClass; |
46 |
| -import org.apache.druid.server.http.security.ConfigResourceFilter; |
47 |
| -import org.apache.druid.server.lookup.cache.LookupCoordinatorManager; |
48 |
| -import org.apache.druid.server.lookup.cache.LookupExtractorFactoryMapContainer; |
49 |
| -import org.apache.druid.server.security.AuthorizationUtils; |
50 |
| - |
51 |
| -import javax.annotation.Nullable; |
52 |
| -import javax.servlet.http.HttpServletRequest; |
53 |
| -import javax.ws.rs.Consumes; |
54 |
| -import javax.ws.rs.DELETE; |
55 |
| -import javax.ws.rs.DefaultValue; |
56 |
| -import javax.ws.rs.GET; |
57 |
| -import javax.ws.rs.POST; |
58 |
| -import javax.ws.rs.Path; |
59 |
| -import javax.ws.rs.PathParam; |
60 |
| -import javax.ws.rs.Produces; |
61 |
| -import javax.ws.rs.QueryParam; |
62 |
| -import javax.ws.rs.core.Context; |
63 |
| -import javax.ws.rs.core.MediaType; |
64 |
| -import javax.ws.rs.core.Response; |
| 33 | +import org.skife.jdbi.v2.Handle; |
| 34 | +import org.skife.jdbi.v2.IDBI; |
| 35 | + |
65 | 36 | import java.io.IOException;
|
66 |
| -import java.io.InputStream; |
67 |
| -import java.util.ArrayList; |
68 |
| -import java.util.Collection; |
69 |
| -import java.util.HashMap; |
70 |
| -import java.util.HashSet; |
71 | 37 | import java.util.List;
|
72 | 38 | import java.util.Map;
|
73 |
| -import java.util.Objects; |
74 |
| -import java.util.Set; |
75 |
| -import java.util.stream.Collectors; |
76 | 39 |
|
77 |
| -/** |
78 |
| - * Contains information about lookups exposed through the coordinator |
79 |
| - */ |
80 |
| -@Path("/druid/coordinator/v1/test") |
81 |
| -@ResourceFilters(ConfigResourceFilter.class) |
82 |
| -public class TestResource1 |
| 40 | +@ManageLifecycle |
| 41 | +public class TestDbClass //implements AuditManager |
83 | 42 | {
|
| 43 | + private final IDBI dbi; |
| 44 | + private final SQLMetadataConnector connector; |
| 45 | + private final Supplier<MetadataStorageTablesConfig> dbTables; |
| 46 | + private final ServiceEmitter emitter; |
| 47 | + private final ObjectMapper jsonMapper; |
| 48 | +// private final SQLAuditManagerConfig config; |
| 49 | +// private final AuditSerdeHelper serdeHelper; |
84 | 50 |
|
85 |
| - private TestDbClass ob; |
| 51 | +// private final ResultSetMapper<AuditEntry> resultMapper; |
86 | 52 |
|
87 | 53 | @Inject
|
88 |
| - public TestResource1( |
89 |
| - TestDbClass ob1 |
90 |
| - ) { |
91 |
| - this.ob = ob1; |
| 54 | + public TestDbClass( |
| 55 | + SQLMetadataConnector connector, |
| 56 | + Supplier<MetadataStorageTablesConfig> dbTables, |
| 57 | + ServiceEmitter emitter, |
| 58 | + @Json ObjectMapper jsonMapper |
| 59 | + ) |
| 60 | + { |
| 61 | + this.dbi = connector.getDBI(); |
| 62 | + this.connector = connector; |
| 63 | + this.dbTables = dbTables; |
| 64 | + this.emitter = emitter; |
| 65 | + this.jsonMapper = jsonMapper; |
| 66 | +// this.serdeHelper = serdeHelper; |
| 67 | +// this.resultMapper = new AuditEntryMapper(); |
| 68 | + |
92 | 69 | }
|
93 | 70 |
|
94 |
| - @GET |
95 |
| - @Path("/p1") |
96 |
| - @Produces({MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE}) |
97 |
| - public Response getTiers( |
| 71 | + @LifecycleStart |
| 72 | + public void start() |
| 73 | + { |
| 74 | +// c onnector.createAuditTable(); |
| 75 | + } |
98 | 76 |
|
99 |
| - ) |
| 77 | + @LifecycleStop |
| 78 | + public void stop() |
100 | 79 | {
|
101 |
| - return Response.ok().entity("Hello!!").build(); |
| 80 | + // Do nothing |
102 | 81 | }
|
103 | 82 |
|
104 |
| - @GET |
105 |
| - @Path("/add") |
106 |
| - @Produces({MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE}) |
107 |
| - public Response add( |
| 83 | + private String getAuditTable() |
| 84 | + { |
| 85 | + return "test_table";//dbTables.get().getAuditTable(); |
| 86 | + } |
108 | 87 |
|
109 |
| - ) |
| 88 | +// @Override |
| 89 | + public void addData() |
110 | 90 | {
|
111 |
| - ob.addData(); |
112 |
| - return Response.ok().entity("Data added!!").build(); |
| 91 | + dbi.withHandle( |
| 92 | + handle -> { |
| 93 | + doAudit("hello", handle); |
| 94 | + return 0; |
| 95 | + } |
| 96 | + ); |
113 | 97 | }
|
114 | 98 |
|
115 |
| - @GET |
116 |
| - @Path("/get") |
117 |
| - @Produces({MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE}) |
118 |
| - public Response getAll( |
| 99 | +// private ServiceMetricEvent.Builder createMetricEventBuilder(AuditEntry entry) |
| 100 | +// { |
| 101 | +// ServiceMetricEvent.Builder builder = new ServiceMetricEvent.Builder() |
| 102 | +// .setDimension("key", entry.getKey()) |
| 103 | +// .setDimension("type", entry.getType()) |
| 104 | +// .setDimension("author", entry.getAuditInfo().getAuthor()) |
| 105 | +// .setDimension("comment", entry.getAuditInfo().getComment()) |
| 106 | +// .setDimension("remote_address", entry.getAuditInfo().getIp()) |
| 107 | +// .setDimension("created_date", entry.getAuditTime().toString()); |
| 108 | +// |
| 109 | +// if (config.isIncludePayloadAsDimensionInMetric()) { |
| 110 | +// builder.setDimension("payload", entry.getPayload().serialized()); |
| 111 | +// } |
| 112 | +// |
| 113 | +// return builder; |
| 114 | +// } |
119 | 115 |
|
120 |
| - ) |
| 116 | +// @Override |
| 117 | + public void doAudit(String data, Handle handle) throws IOException |
| 118 | + { |
| 119 | + |
| 120 | + handle.createStatement( |
| 121 | + StringUtils.format( |
| 122 | + "INSERT INTO %s (c1)" |
| 123 | + + " VALUES (:audit_key)", |
| 124 | + getAuditTable() |
| 125 | + ) |
| 126 | + ) |
| 127 | + .bind("audit_key", "Hello") |
| 128 | +// .bind("type", record.getType()) |
| 129 | +// .bind("author", record.getAuditInfo().getAuthor()) |
| 130 | +// .bind("comment", record.getAuditInfo().getComment()) |
| 131 | +// .bind("created_date", record.getAuditTime().toString()) |
| 132 | +// .bind("payload", jsonMapper.writeValueAsBytes(record)) |
| 133 | + .execute(); |
| 134 | + } |
| 135 | + |
| 136 | + public List<Map<String, Object>> getAllData() |
121 | 137 | {
|
122 |
| -// return ob.getAllData(); |
123 |
| - return Response.ok().entity(ob.getAllData()).build(); |
| 138 | + return dbi.withHandle( |
| 139 | + (Handle handle) -> handle |
| 140 | + .createQuery( |
| 141 | + StringUtils.format( |
| 142 | + "SELECT c1 FROM %s", |
| 143 | + getAuditTable() |
| 144 | + ) |
| 145 | + ) |
| 146 | + .list() |
| 147 | + ); |
124 | 148 | }
|
| 149 | +// |
| 150 | +// @Override |
| 151 | +// public List<AuditEntry> fetchAuditHistory(final String key, final String type, Interval interval) |
| 152 | +// { |
| 153 | +// final Interval theInterval = createAuditHistoryIntervalIfNull(interval); |
| 154 | +// return dbi.withHandle( |
| 155 | +// (Handle handle) -> handle |
| 156 | +// .createQuery( |
| 157 | +// StringUtils.format( |
| 158 | +// "SELECT payload FROM %s WHERE audit_key = :audit_key and type = :type and " |
| 159 | +// + "created_date between :start_date and :end_date ORDER BY created_date", |
| 160 | +// getAuditTable() |
| 161 | +// ) |
| 162 | +// ) |
| 163 | +// .bind("audit_key", key) |
| 164 | +// .bind("type", type) |
| 165 | +// .bind("start_date", theInterval.getStart().toString()) |
| 166 | +// .bind("end_date", theInterval.getEnd().toString()) |
| 167 | +// .map(resultMapper) |
| 168 | +// .list() |
| 169 | +// ); |
| 170 | +// } |
| 171 | +// |
| 172 | +// private Interval createAuditHistoryIntervalIfNull(Interval interval) |
| 173 | +// { |
| 174 | +// if (interval == null) { |
| 175 | +// DateTime now = DateTimes.nowUtc(); |
| 176 | +// return new Interval(now.minus(config.getAuditHistoryMillis()), now); |
| 177 | +// } else { |
| 178 | +// return interval; |
| 179 | +// } |
| 180 | +// } |
| 181 | +// |
| 182 | +// private int getLimit(int limit) throws IllegalArgumentException |
| 183 | +// { |
| 184 | +// if (limit < 1) { |
| 185 | +// throw new IllegalArgumentException("Limit must be greater than zero!"); |
| 186 | +// } |
| 187 | +// return limit; |
| 188 | +// } |
| 189 | +// |
| 190 | +// @Override |
| 191 | +// public List<AuditEntry> fetchAuditHistory(final String type, Interval interval) |
| 192 | +// { |
| 193 | +// final Interval theInterval = createAuditHistoryIntervalIfNull(interval); |
| 194 | +// return dbi.withHandle( |
| 195 | +// (Handle handle) -> handle |
| 196 | +// .createQuery( |
| 197 | +// StringUtils.format( |
| 198 | +// "SELECT payload FROM %s WHERE type = :type and created_date between :start_date and " |
| 199 | +// + ":end_date ORDER BY created_date", |
| 200 | +// getAuditTable() |
| 201 | +// ) |
| 202 | +// ) |
| 203 | +// .bind("type", type) |
| 204 | +// .bind("start_date", theInterval.getStart().toString()) |
| 205 | +// .bind("end_date", theInterval.getEnd().toString()) |
| 206 | +// .map(resultMapper) |
| 207 | +// .list() |
| 208 | +// ); |
| 209 | +// } |
| 210 | +// |
| 211 | +// @Override |
| 212 | +// public List<AuditEntry> fetchAuditHistory(final String key, final String type, int limit) |
| 213 | +// throws IllegalArgumentException |
| 214 | +// { |
| 215 | +// return fetchAuditHistoryLastEntries(key, type, limit); |
| 216 | +// } |
| 217 | +// |
| 218 | +// @Override |
| 219 | +// public List<AuditEntry> fetchAuditHistory(final String type, int limit) |
| 220 | +// throws IllegalArgumentException |
| 221 | +// { |
| 222 | +// return fetchAuditHistoryLastEntries(null, type, limit); |
| 223 | +// } |
| 224 | +// |
| 225 | +// @Override |
| 226 | +// public int removeAuditLogsOlderThan(final long timestamp) |
| 227 | +// { |
| 228 | +// DateTime dateTime = DateTimes.utc(timestamp); |
| 229 | +// return dbi.withHandle( |
| 230 | +// handle -> { |
| 231 | +// Update sql = handle.createStatement( |
| 232 | +// StringUtils.format( |
| 233 | +// "DELETE FROM %s WHERE created_date < :date_time", |
| 234 | +// getAuditTable() |
| 235 | +// ) |
| 236 | +// ); |
| 237 | +// return sql.bind("date_time", dateTime.toString()) |
| 238 | +// .execute(); |
| 239 | +// } |
| 240 | +// ); |
| 241 | +// } |
| 242 | +// |
| 243 | +// private List<AuditEntry> fetchAuditHistoryLastEntries(final String key, final String type, int limit) |
| 244 | +// throws IllegalArgumentException |
| 245 | +// { |
| 246 | +// final int theLimit = getLimit(limit); |
| 247 | +// String queryString = StringUtils.format("SELECT payload FROM %s WHERE type = :type", getAuditTable()); |
| 248 | +// if (key != null) { |
| 249 | +// queryString += " and audit_key = :audit_key"; |
| 250 | +// } |
| 251 | +// queryString += " ORDER BY created_date DESC"; |
| 252 | +// final String theQueryString = queryString; |
| 253 | +// |
| 254 | +// return dbi.withHandle( |
| 255 | +// (Handle handle) -> { |
| 256 | +// Query<Map<String, Object>> query = handle.createQuery(theQueryString); |
| 257 | +// if (key != null) { |
| 258 | +// query.bind("audit_key", key); |
| 259 | +// } |
| 260 | +// return query |
| 261 | +// .bind("type", type) |
| 262 | +// .setMaxRows(theLimit) |
| 263 | +// .map(resultMapper) |
| 264 | +// .list(); |
| 265 | +// } |
| 266 | +// ); |
| 267 | +// } |
| 268 | +// |
| 269 | +// private class AuditEntryMapper implements ResultSetMapper<AuditEntry> |
| 270 | +// { |
| 271 | +// @Override |
| 272 | +// public AuditEntry map(int index, ResultSet r, StatementContext ctx) throws SQLException |
| 273 | +// { |
| 274 | +// return JacksonUtils.readValue(jsonMapper, r.getBytes("payload"), AuditEntry.class); |
| 275 | +// } |
| 276 | +// } |
| 277 | + |
125 | 278 | }
|
0 commit comments