- private class StopWatch {
- private long clock = 0;
- private long elapsed = 0;
-
-
-
- public StopWatch() {
- clock = 0;
- elapsed = 0;
- }
-
- public void reset() {
- clock = System.currentTimeMillis();
- elapsed = 0;
- }
- public void stop() {
- Long stopTime = System.currentTimeMillis();
- elapsed += stopTime - clock;
- clock = 0;
- MDC.put( MDC_END_TIMESTAMP, isoFormatter.format(new Date(stopTime)));
- MDC.put( MDC_ELAPSED_TIME, String.valueOf(elapsed));
- }
- public void start() {
- if ( clock != 0 ) {
- //not stopped
- return;
- }
- clock = System.currentTimeMillis();
- MDC.put( MDC_BEGIN_TIMESTAMP, isoFormatter.format(new Date(clock)));
- }
- private long getElapsed() {
- return elapsed;
- }
- }
-
- private String apiNamespace;
-
- private String uri;
- private String uriPath;
- private String method;
- private String authorization;
- private String requestId;
- private ApiError err;
- private StopWatch stopwatch;
- private ApiPolicy apiPolicy;
-
- public static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
- public final static TimeZone utc = TimeZone.getTimeZone("UTC");
- public final static SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
-
- static {
- isoFormatter.setTimeZone(utc);
- }
- public ApiService() {
-
- stopwatch = new StopWatch();
- stopwatch.start();
- err = new ApiError();
- requestId = (new RandomString(10)).nextString();
-
- if (apiNamespace == null) {
- DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
- apiNamespace = p.getProperty("ApiNamespace", "org.openecomp.dmaapBC.api");
- logger.info( "config param usePE has been deprecated. Use ApiPermission.Class property instead.");
- }
- apiPolicy = new ApiPolicy();
-
- logger.info( "apiNamespace=" + apiNamespace);
- }
-
- public ApiService setAuth( String auth ) {
- this.authorization = auth;
- logger.info( "setAuth: authorization={} ", authorization);
- return this;
- }
- private void setServiceName(){
- String svcRequest = new String( this.method + " " + this.uriPath );
- MDC.put(MDC_SERVICE_NAME, svcRequest );
- }
- public ApiService setHttpMethod( String httpMethod ) {
- this.method = httpMethod;
- logger.info( "setHttpMethod: method={} ", method);
- setServiceName();
- return this;
- }
- public ApiService setUriPath( String uriPath ) {
- this.uriPath = uriPath;
- this.uri = setUriFromPath( uriPath );
- logger.info( "setUriPath: uriPath={} uri={}", uriPath, uri);
- setServiceName();
- return this;
- }
- private String setUriFromPath( String uriPath ) {
- int ch = uriPath.indexOf("/");
- if ( ch > 0 ) {
- return( (String) uriPath.subSequence(0, ch ) );
- } else {
- return uriPath;
- }
- }
-
- public ApiError getErr() {
- return err;
- }
-
-
- public void setErr(ApiError err) {
- this.err = err;
- }
-
-
- // test for presence of a required field
- public void required( String name, Object val, String expr ) throws RequiredFieldException {
- err.setCode(0);
- if ( val == null ) {
- err.setCode(Status.BAD_REQUEST.getStatusCode());
- err.setMessage("missing required field");
- err.setFields( name );
- throw new RequiredFieldException();
- }
- if ( expr != null && ! expr.isEmpty() ) {
- Pattern pattern = Pattern.compile(expr);
- Matcher matcher = pattern.matcher((CharSequence) val);
- if ( ! matcher.find() ) {
- err.setCode(Status.BAD_REQUEST.getStatusCode());
- err.setMessage( "value '" + val + "' violates regexp check '" + expr + "'");
- err.setFields( name );
- throw new RequiredFieldException();
- }
- }
- }
-
- // utility to serialize ApiErr object
- public String toString() {
- return String.format( "code=%d msg=%s fields=%s", err.getCode(), err.getMessage(), err.getFields() );
- }