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 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); } }
default CompletableFuture<int[]> storeBatchAsync(List<Event> events) { try { return CompletableFuture.supplyAsync(() -> storeBatch(events), workerGroup); } catch (RejectedExecutionException e) { throw new RakamException("The server is busy, please try again later", INTERNAL_SERVER_ERROR); } }
protected boolean testDeviceIdExists(FunnelStep firstAction, Map<String, List<SchemaField>> collections) { List<SchemaField> schemaFields = collections.get(firstAction.getCollection()); if (schemaFields == null) { throw new RakamException("The collection in first action does not exist.", HttpResponseStatus.BAD_REQUEST); } return schemaFields.stream().anyMatch(e -> e.getName().equals("_device_id")); } }
@Override public QueryExecution query(RequestContext context, List<FunnelStep> steps, Optional<String> dimension, Optional<String> segment, LocalDate startDate, LocalDate endDate, Optional<FunnelWindow> window, ZoneId zoneId, Optional<List<String>> connectors, FunnelType funnelType) { if (funnelType != FunnelType.ORDERED) { return fastExecutor.query(context, steps, dimension, segment, startDate, endDate, window, zoneId, connectors, funnelType); } if (dimension.isPresent() && projectConfig.getUserColumn().equals(dimension.get())) { throw new RakamException("Dimension and connector field cannot be equal", HttpResponseStatus.BAD_REQUEST); } return super.query(context, steps, dimension, segment, startDate, endDate, window, zoneId, connectors, funnelType); }
@Path("/get") @ApiOperation(value = "Get Report", authorizations = @Authorization(value = "read_key")) @JsonRequest public Map<String, String> get(@com.google.inject.name.Named("user_id") UIPermissionParameterProvider.Project project, @ApiParam("slug") String slug) { if (!database.isPresent()) { throw new RakamException("Custom page feature is not implemented", NOT_IMPLEMENTED); } return database.get().get(project.project, slug); }
@JsonRequest @ApiOperation(value = "Create multiple new users", authorizations = @Authorization(value = "write_key"), notes = "Returns user ids. User id may be string or numeric.") @Path("/batch/create") public List<Object> createUsers(@Named("project") RequestContext context, @ApiParam("users") List<User> users) { try { return service.batchCreate(context, users); } catch (Exception e) { throw new RakamException(e.getMessage(), BAD_REQUEST); } }
public SuccessMessage testDatabase(String project, String type, JDBCSchemaConfig options) { SupportedCustomDatabase optionalFunction = SupportedCustomDatabase.getAdapter(type); Optional<String> test = optionalFunction.getDataSource().test(options); if (test.isPresent()) { throw new RakamException(test.get(), BAD_REQUEST); } return SuccessMessage.success(); }
public SuccessMessage testFile(String project, DiscoverableRemoteTable hook) { ExternalFileCustomDataSource source = new ExternalFileCustomDataSource(); Optional<String> test = source.test(hook.getTable()); if (test.isPresent()) { throw new RakamException(test.get(), BAD_REQUEST); } return SuccessMessage.success(); }
@JsonRequest @ProtectEndpoint(writeOperation = true, requiresProject = false) @Path("/get-lock-key") public String getLockKey(@ApiParam("api_url") String apiUrl, @javax.inject.Named("user_id") Project project) { Optional<WebUser> webUser = service.getUser(project.userId); if (webUser.get().readOnly) { throw new RakamException("User is not allowed to create projects", UNAUTHORIZED); } return service.getLockKeyForAPI(project.userId, apiUrl); }
@ApiOperation(value = "List created projects", authorizations = @Authorization(value = "read_key") ) @JsonRequest @Path("/list") public Set<String> getProjects(@ApiParam(value = "lock_key", required = false) String lockKey) { if (!Objects.equals(projectConfig.getLockKey(), lockKey)) { throw new RakamException("Lock key is invalid", FORBIDDEN); } return metastore.getProjects(); }
public void merge(String project, Object user, Object anonymousId, Instant createdAt, Instant mergedAt) { if (!config.getEnableUserMapping()) { throw new RakamException(NOT_IMPLEMENTED); } GenericData.Record properties = new GenericData.Record(ANONYMOUS_USER_MAPPING_SCHEMA); properties.put(0, anonymousId); properties.put(1, user); properties.put(2, createdAt.toEpochMilli()); properties.put(3, mergedAt.toEpochMilli()); eventStore.store(new Event(project, ANONYMOUS_ID_MAPPING, null, null, properties)); } }
@JsonRequest @ApiOperation(value = "Update project configurations") @Path("/project-configuration") public SuccessMessage updateProjectPreferences(@Named("user_id") Project project, @BodyParam ProjectConfiguration configuration) { if (!service.hasMasterAccess(project.project, project.userId)) { throw new RakamException("You don't have master access", UNAUTHORIZED); } service.updateProjectConfigurations(project.project, configuration); return SuccessMessage.success(); }
@Override public CompletableFuture<QueryResult> delete(RequestContext context, String name) { MaterializedView materializedView = database.getMaterializedView(context.project, name); database.deleteMaterializedView(context.project, name); String reference = queryExecutor.formatTableReference(context.project, QualifiedName.of("materialized", materializedView.tableName), Optional.empty(), ImmutableMap.of()); return queryExecutor.executeRawQuery(context, format("DROP TABLE %s", reference)).getResult().thenApply(result -> { if (result.isFailed()) { throw new RakamException("Error while deleting materialized table: " + result.getError().toString(), INTERNAL_SERVER_ERROR); } return result; }); }
public SuccessMessage removeFile(String project, String tableName) { try (Handle handle = dbi.open()) { int execute = handle.createStatement("DELETE FROM custom_file_source WHERE project = :project AND table_name = :table_name") .bind("project", project) .bind("table_name", tableName) .execute(); if (execute == 0) { throw new RakamException("Custom file not found", NOT_FOUND); } return SuccessMessage.success(); } }
public SuccessMessage removeDatabase(String project, String schemaName) { try (Handle handle = dbi.open()) { int execute = handle.createStatement("DELETE FROM custom_data_source WHERE project = :project AND schema_name = :schema_name") .bind("project", project) .bind("schema_name", schemaName) .execute(); if (execute == 0) { throw new RakamException("Custom database not found", NOT_FOUND); } return SuccessMessage.success(); } }