Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.agilecoders.wicket.logging;
  
 
 import java.util.Map;

This behavior renders the necessary js file and initializer script to the markup and handles incoming log requests.

Author(s):
miha
 
 public class ClientSideLoggingBehavior extends Behavior {
 
     private final Map<StringObjectdata;

    

Returns:
current active settings
 
     private static ClientSideLoggingSettings settings() {
         return ClientSideLoggingSettings.get();
     }

    

Returns:
new builder object.
 
     public static Builder newBuilder() {
         return new Builder();
     }

    

Parameters:
spec the behavior specification
Returns:
new builder object.
 
     public static Builder newBuilder(final String spec) {
         return new Builder(SpecBuilder.parse(spec).data());
     }

    
The builder to configure this behavior
 
     public static final class Builder {
         private final Map<StringObjectdata;
 
         private Builder() {
             this(new HashMap<StringObject>());
         }
 
         private Builder(Map<StringObjectdata) {
             this. = data;
         }
 
         public Builder replaceWicketLog(final boolean value) {
             .put("replaceWicketLog"value);
 
             if (. == value) {
                 .remove("replaceWicketLog");
             }
 
             return this;
         }

        
if set to "FALSE", only the first window.onerror will be sent to server.

Parameters:
value whether to log all window.onerror or not
Returns:
this instance for chaining
Since:
0.1.3
 
         public Builder logAdditionalErrors(final boolean value) {
             .put("logAdditionalErrors"value);
 
             if (. == value) {
                 .remove("logAdditionalErrors");
             }
 
             return this;
         }
 
         public Builder replaceWindowOnError(final boolean value) {
             .put("replaceWindowOnError"value);
            if (. == value) {
                .remove("replaceWindowOnError");
            }
            return this;
        }
        public Builder wrapWicketLog(final boolean value) {
            .put("wrapWicketLog"value);
            if (. == value) {
                .remove("wrapWicketLog");
            }
            return this;
        }
        public Builder wrapWindowOnError(final boolean value) {
            .put("wrapWindowOnError"value);
            if (. == value) {
                .remove("wrapWindowOnError");
            }
            return this;
        }
        public Builder flushMessagesOnUnload(final boolean value) {
            .put("flushMessagesOnUnload"value);
            if (. == value) {
                .remove("flushMessagesOnUnload");
            }
            return this;
        }
        public Builder collectClientInfos(final boolean value) {
            .put("collectClientInfos"value);
            if (. == value) {
                .remove("collectClientInfos");
            }
            return this;
        }
        public Builder collectionTimer(final Duration value) {
            .put("collectionTimer"value.getMilliseconds());
            if (. == value.getMilliseconds()) {
                .remove("collectionTimer");
            }
            return this;
        }
        public Builder collectionType(final CollectionType value) {
            .put("collectionType"value.asString());
            if (..equals(value)) {
                .remove("collectionType");
            }
            return this;
        }
        public Builder loggerName(final String value) {
            .put("loggerName"value);
            if (..equals(value)) {
                .remove("loggerName");
            }
            return this;
        }
        public Builder maxQueueSize(final int value) {
            .put("maxQueueSize"value);
            if (. == value) {
                .remove("maxQueueSize");
            }
            return this;
        }
        public Builder customFilter(final String value) {
            .put("customFilter"value);
            if (value == null) {
                .remove("customFilter");
            }
            return this;
        }

        

Returns:
copy of builder data
        public Map<StringObjectdata() {
            return new HashMap<>();
        }

        
        public ClientSideLoggingBehavior build() {
            return new ClientSideLoggingBehavior(data());
        }

        

Returns:
a new instance of given ClientSideLoggingBehavior class. This method can be used to build sub classes of ClientSideLoggingBehavior.
        public ClientSideLoggingBehavior build(Class<? extends ClientSideLoggingBehaviorclazz) {
            try {
                Constructor<? extends ClientSideLoggingBehaviorconstructor = clazz.getConstructor(Map.class);
                return Args.notNull(constructor"you have to create a public constructor with one parameter of type Map.class").newInstance();
                throw new WicketRuntimeException(e);
            }
        }
    }

    
Construct.
    public ClientSideLoggingBehavior() {
        this(new HashMap<StringObject>());
    }

    
Construct.

Parameters:
data initial configuration to use
    public ClientSideLoggingBehavior(final Map<StringObjectdata) {
        data.put("url"createCallbackUrl());
        data.put("logLevel"settings().level());
        if (settings().logStacktrace() != .) {
            data.put("logStacktrace"settings().logStacktrace());
        }
        if (settings().debug()) {
            data.put("debug"true);
        }
        this. = data;
    }
    @Override
    public void renderHead(Component componentIHeaderResponse response) {
        final ClientSideLoggingSettings settings = settings();
        response.render(JavaScriptHeaderItem.forReference(Application.get().getJavaScriptLibrarySettings().getJQueryReference()));
        response.render(JavaScriptHeaderItem.forReference(WicketAjaxJQueryResourceReference.get()));
        if (ClientSideLoggingSettings.get().logStacktrace()) {
            response.render(JavaScriptHeaderItem.forReference(new WebjarsJavaScriptResourceReference("stacktrace/current/stacktrace.js")));
        }

        
amplify js is used as wrapper for localStorage because each browser comes with its own implementation
        if (..asString().equals(.get("collectionType"))) {
            response.render(JavaScriptHeaderItem.forReference(new WebjarsJavaScriptResourceReference("amplifyjs/current/amplify.store.min.js")));
        }
        response.render(settings.javaScriptHeaderItem());
        response.render(newHeaderItem(createInitializerScript(), settings.id()));
    }

    
creates a new header item for the initializer script

Parameters:
script the script to render
id the js id that can be used when rendering this script
Returns:
new header item
    protected HeaderItem newHeaderItem(final CharSequence scriptfinal String id) {
        return OnDomReadyHeaderItem.forScript(script);
    }

    
creates the initializer script

Parameters:
data configuration data
Returns:
new initializer script.
    protected CharSequence createInitializerScript(final Map<StringObjectdata) {
        try {
            return "window.wicketClientSideLogging(jQuery, Wicket, window.amplify, " + JSONObject.valueToString(data) + ");";
        } catch (JSONException e) {
            throw new WicketRuntimeException(e);
        }
    }

    

Returns:
callback url that is used as client side logging entry
    protected CharSequence createCallbackUrl() {
        return RequestCycle.get().urlFor(new ClientSideErrorLoggingResourceReference(), null);
    }
New to GrepCode? Check out our FAQ X