@Override public void store(Event event) { throw new RakamException(NOT_IMPLEMENTED); }
public static void logException(RakamHttpRequest request, RakamException e) { if (RAVEN == null) { return; } EventBuilder builder = new EventBuilder() .withMessage(e.getMessage()) .withSentryInterface(new HttpInterface(new RakamServletWrapper(request))) .withLevel(Event.Level.ERROR) .withLogger(RakamException.class.getName()) .withTag("status", e.getStatusCode().reasonPhrase()); if (TAGS != null) { for (Map.Entry<String, String> entry : TAGS.entrySet()) { builder.withTag(entry.getKey(), entry.getValue()); } } if (RELEASE != null) { builder.withRelease(RELEASE); } RAVEN.sendEvent(builder.build()); }
@ApiOperation(value = "Get project stats") @JsonRequest @Path("/stats") public Map<String, Metastore.Stats> getStats(@BodyParam List<String> apiKeys) { Map<String, String> keys = new LinkedHashMap<>(); for (String apiKey : apiKeys) { String project; try { project = apiKeyService.getProjectOfApiKey(apiKey, READ_KEY); } catch (RakamException e) { if (e.getStatusCode() == FORBIDDEN) { continue; } throw e; } keys.put(project, apiKey); } Map<String, Metastore.Stats> stats = metastore.getStats(keys.keySet()); if (stats == null) { return ImmutableMap.of(); } return stats.entrySet().stream() .collect(Collectors.toMap(e -> keys.get(e.getKey()), e -> e.getValue())); }
@Override public void createMaterializedView(String project, MaterializedView materializedView) { try (Handle handle = dbi.open()) { try { handle.createStatement("INSERT INTO materialized_views (project, name, query, table_name, update_interval, incremental, real_time, options) " + "VALUES (:project, :name, :query, :table_name, :update_interval, :incremental, :real_time, :options)") .bind("project", project) .bind("name", materializedView.name) .bind("table_name", materializedView.tableName) .bind("query", materializedView.query) .bind("update_interval", materializedView.updateInterval != null ? materializedView.updateInterval.toMillis() : null) .bind("incremental", materializedView.incremental) .bind("real_time", materializedView.realTime) .bind("options", JsonHelper.encode(materializedView.options)) .execute(); } catch (Exception e) { try { getMaterializedView(project, materializedView.tableName); } catch (RakamException e1) { if (e1.getStatusCode() == NOT_FOUND) { throw Throwables.propagate(e1); } } throw new AlreadyExistsException("Materialized view", BAD_REQUEST); } } }
@Override protected String visitNode(Node node, Boolean context) { throw new RakamException("The filter syntax is not supported", BAD_REQUEST); }
public SuccessMessage addDatabase(String project, CustomDataSource hook) { try (Handle handle = dbi.open()) { try { handle.createStatement("INSERT INTO custom_data_source (project, schema_name, type, options) " + "VALUES (:project, :schema_name, :type, :options)") .bind("project", project) .bind("schema_name", hook.schemaName) .bind("type", hook.type) .bind("options", JsonHelper.encode(hook.options)) .execute(); return SuccessMessage.success(); } catch (Exception e) { try { getDatabase(project, hook.schemaName); throw new AlreadyExistsException("Custom database", BAD_REQUEST); } catch (RakamException e1) { if (e1.getStatusCode() != NOT_FOUND) { throw e1; } else { throw e; } } } } }
@Override public int[] storeBatch(List<Event> events) { throw new RakamException(NOT_IMPLEMENTED); }
public SuccessMessage addFile(String project, @ApiParam("tableName") String tableName, @ApiParam("options") DiscoverableRemoteTable hook) { try (Handle handle = dbi.open()) { try { handle.createStatement("INSERT INTO custom_file_source (project, table_name, options) " + "VALUES (:project, :table_name, :options)") .bind("project", project) .bind("table_name", tableName) .bind("options", JsonHelper.encode(hook.getTable())) .execute(); return SuccessMessage.success(); } catch (Exception e) { try { getFile(project, tableName); throw new AlreadyExistsException("Custom file", BAD_REQUEST); } catch (RakamException e1) { if (e1.getStatusCode() != NOT_FOUND) { throw e1; } else { throw e; } } } } }
@Override public void createSegment(RequestContext context, String name, String tableName, Expression filterExpression, List<EventFilter> eventFilter, Duration interval) { throw new RakamException("Unsupported", HttpResponseStatus.BAD_REQUEST); }
public static void checkArgument(boolean expression, @Nullable String errorMessage) { if (!expression) { if (errorMessage == null) { throw new RakamException(BAD_REQUEST); } else { throw new RakamException(errorMessage, BAD_REQUEST); } } }
@Override public InputStream getFile(int project, String name, String file) { File f = new File(directory, project + File.separator + name + File.separator + file); try { return new ByteArrayInputStream(Files.readAllBytes(f.toPath())); } catch (NoSuchFileException e) { throw new RakamException("File not found", HttpResponseStatus.NOT_FOUND); } catch (IOException e) { throw Throwables.propagate(e); } }
private String getParam(Map<String, List<String>> params, String param) { List<String> strings = params.get(param); if (strings == null || strings.size() == 0) { throw new RakamException(String.format("%s query parameter is required", param), BAD_REQUEST); } return strings.get(strings.size() - 1); }
public static String encryptToHex(String data, String secret, String hashType) { try { if (secret == null || secret.isEmpty()) { throw new RakamException("Secret is is empty", INTERNAL_SERVER_ERROR); } SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes("UTF-8"), hashType); Mac mac = Mac.getInstance(hashType); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8")); return DatatypeConverter.printHexBinary(rawHmac).toLowerCase(Locale.ENGLISH); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { throw Throwables.propagate(e); } }
@Override public List<Cookie> map(String project, List<? extends ISingleUserBatchOperation> user, RequestParams requestParams, InetAddress sourceAddress) { for (ISingleUserBatchOperation operation : user) { if (operation.getUser() == null) { throw new RakamException("_user cannot be null", BAD_REQUEST); } } return null; } }
public NashornHttpRequest(Request.Builder requestBuilder, String method, String url) { this.requestBuilder = requestBuilder; this.method = method; try { this.url = HttpUrl.get(new URL(url)); } catch (MalformedURLException e) { throw new RakamException("URL is not valid", HttpResponseStatus.BAD_REQUEST); } }
@JsonIgnore public synchronized Optional<Expression> getExpression() { try { return filterExpression.map(value -> parser.createExpression(value)); } catch (Exception e) { throw new RakamException("Unable to parse filter expression: " + filterExpression.get(), HttpResponseStatus.BAD_REQUEST); } } }
public PrestoQueryExecution(ClientSession session, String query, boolean update) { this.startTime = Instant.now(); this.query = query; this.update = update; try { QUERY_EXECUTOR.execute(new QueryTracker(session)); } catch (RejectedExecutionException e) { throw new RakamException("There are already 1000 running queries. Please calm down.", HttpResponseStatus.TOO_MANY_REQUESTS); } result = new CompletableFuture<>(); }
@JsonCreator public static TimestampTransformation fromString(String key) { try { return key == null ? null : valueOf(key.toUpperCase()); } catch (IllegalArgumentException e) { throw new RakamException("Invalid date range value: " + key, BAD_REQUEST); } }
@JsonIgnore public synchronized Expression getExpression() { try { return filterExpression != null ? SQL_PARSER.createExpression(filterExpression) : null; } catch (Exception e) { throw new RakamException(format("filter expression '%s' couldn't parsed", filterExpression), HttpResponseStatus.BAD_REQUEST); } }
default CompletableFuture<Void> storeAsync(Event event) { try { return CompletableFuture.supplyAsync(() -> { store(event); return null; }, workerGroup); } catch (RejectedExecutionException e) { throw new RakamException("The server is busy, please try again later", INTERNAL_SERVER_ERROR); } }