From 2f96f5aceec9a49b9f605ee0e5a8fa9674f1cab4 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Tue, 22 Jan 2013 12:08:10 +0000 Subject: [PATCH] add a remote log to log from javascript to server log git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1436858 13f79535-47bb-0310-9956-ffa450edef68 --- .../web/api/DefaultJavascriptLogger.java | 80 +++++++++++++++++++ .../archiva/web/api/JavascriptLogger.java | 73 +++++++++++++++++ .../archiva/web/model/JavascriptLog.java | 72 +++++++++++++++++ .../resources/META-INF/spring-context.xml | 1 + .../src/main/webapp/js/archiva/utils.js | 47 +++++++++++ 5 files changed, 273 insertions(+) create mode 100644 archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultJavascriptLogger.java create mode 100644 archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/JavascriptLogger.java create mode 100644 archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/model/JavascriptLog.java diff --git a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultJavascriptLogger.java b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultJavascriptLogger.java new file mode 100644 index 000000000..0594a5f18 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultJavascriptLogger.java @@ -0,0 +1,80 @@ +package org.apache.archiva.web.api; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.web.model.JavascriptLog; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +/** + * @author Olivier Lamy + * @since 1.4-M4 + */ +@Service( "javascriptLogger#default" ) +public class DefaultJavascriptLogger + implements JavascriptLogger +{ + private Logger logger = LoggerFactory.getLogger( getClass() ); + + public Boolean trace( JavascriptLog javascriptLog ) + { + Logger toUse = + javascriptLog.getLoggerName() == null ? logger : LoggerFactory.getLogger( javascriptLog.getLoggerName() ); + + toUse.trace( javascriptLog.getMessage() ); + return Boolean.TRUE; + } + + public Boolean debug( JavascriptLog javascriptLog ) + { + Logger toUse = + javascriptLog.getLoggerName() == null ? logger : LoggerFactory.getLogger( javascriptLog.getLoggerName() ); + + toUse.debug( javascriptLog.getMessage() ); + return Boolean.TRUE; + } + + public Boolean info( JavascriptLog javascriptLog ) + { + Logger toUse = + javascriptLog.getLoggerName() == null ? logger : LoggerFactory.getLogger( javascriptLog.getLoggerName() ); + + toUse.info( javascriptLog.getMessage() ); + return Boolean.TRUE; + } + + public Boolean warn( JavascriptLog javascriptLog ) + { + Logger toUse = + javascriptLog.getLoggerName() == null ? logger : LoggerFactory.getLogger( javascriptLog.getLoggerName() ); + + toUse.warn( javascriptLog.getMessage() ); + return Boolean.TRUE; + } + + public Boolean error( JavascriptLog javascriptLog ) + { + Logger toUse = + javascriptLog.getLoggerName() == null ? logger : LoggerFactory.getLogger( javascriptLog.getLoggerName() ); + + toUse.error( javascriptLog.getMessage() ); + return Boolean.TRUE; + } +} diff --git a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/JavascriptLogger.java b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/JavascriptLogger.java new file mode 100644 index 000000000..cb205edbc --- /dev/null +++ b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/JavascriptLogger.java @@ -0,0 +1,73 @@ +package org.apache.archiva.web.api; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.redback.authorization.RedbackAuthorization; +import org.apache.archiva.web.model.JavascriptLog; + +import javax.ws.rs.Consumes; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +/** + * @author Olivier Lamy + * @since 1.4-M4 + */ +@Path( "/javascriptLogger/" ) +public interface JavascriptLogger +{ + + @PUT + @Path( "trace" ) + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization( noRestriction = true, noPermission = true) + Boolean trace( JavascriptLog javascriptLog ); + + @PUT + @Path( "debug" ) + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization( noRestriction = true, noPermission = true) + Boolean debug( JavascriptLog javascriptLog ); + + @PUT + @Path( "info" ) + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization( noRestriction = true, noPermission = true) + Boolean info( JavascriptLog javascriptLog ); + + @PUT + @Path( "warn" ) + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization( noRestriction = true, noPermission = true) + Boolean warn( JavascriptLog javascriptLog ); + + @PUT + @Path( "error" ) + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization( noRestriction = true, noPermission = true) + Boolean error( JavascriptLog javascriptLog ); + +} diff --git a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/model/JavascriptLog.java b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/model/JavascriptLog.java new file mode 100644 index 000000000..8c83c6fe9 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/model/JavascriptLog.java @@ -0,0 +1,72 @@ +package org.apache.archiva.web.model; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; + +/** + * @author Olivier Lamy + * @since 1.4-M4 + */ +@XmlRootElement( name = "javascriptLog" ) +public class JavascriptLog + implements Serializable +{ + + private String loggerName; + + private String message; + + public JavascriptLog() + { + // no op + } + + public String getLoggerName() + { + return loggerName; + } + + public void setLoggerName( String loggerName ) + { + this.loggerName = loggerName; + } + + public String getMessage() + { + return message; + } + + public void setMessage( String message ) + { + this.message = message; + } + + @Override + public String toString() + { + final StringBuilder sb = new StringBuilder(); + sb.append( "JavascriptLog" ); + sb.append( "{loggerName='" ).append( loggerName ).append( '\'' ); + sb.append( ", message='" ).append( message ).append( '\'' ); + sb.append( '}' ); + return sb.toString(); + } +} diff --git a/archiva-modules/archiva-web/archiva-web-common/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-web/archiva-web-common/src/main/resources/META-INF/spring-context.xml index 4df10a85b..95179f71a 100755 --- a/archiva-modules/archiva-web/archiva-web-common/src/main/resources/META-INF/spring-context.xml +++ b/archiva-modules/archiva-web/archiva-web-common/src/main/resources/META-INF/spring-context.xml @@ -51,6 +51,7 @@ + diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/js/archiva/utils.js b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/js/archiva/utils.js index cf0bc6ab8..6bca0fdbf 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/js/archiva/utils.js +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/js/archiva/utils.js @@ -495,6 +495,53 @@ require(["jquery","jquery.tmpl","i18n","knockout"], function(jquery,jqueryTmpl,i mainContent.find(".tooltip-doc" ).tooltip({html: true, trigger: 'hover'}); } + //------------------------------------ + // remote logging + //------------------------------------ + JavascriptLog=function(loggerName,message){ + this.loggerName=loggerName; + this.message=message; + } + + remoteLogTrace=function(loggerName,message){ + var javascriptLog=new JavascriptLog(loggerName,message); + remoteLog("trace",javascriptLog); + } + + remoteLogDebug=function(loggerName,message){ + var javascriptLog=new JavascriptLog(loggerName,message); + remoteLog("debug",javascriptLog); + } + + remoteLogInfo=function(loggerName,message){ + var javascriptLog=new JavascriptLog(loggerName,message); + remoteLog("info",javascriptLog); + } + + remoteLogWarn=function(loggerName,message){ + var javascriptLog=new JavascriptLog(loggerName,message); + remoteLog("warn",javascriptLog); + } + + remoteLogError=function(loggerName,message){ + var javascriptLog=new JavascriptLog(loggerName,message); + remoteLog("error",javascriptLog); + } + + /** + * + * @param level trace/debug/info/warn/error + * @param javascriptLog + */ + remoteLog=function(level,javascriptLog){ + $.ajax("restServices/archivaUiServices/javascriptLogger/"+level,{ + type: "PUT", + contentType: 'application/json', + data: $.toJSON(javascriptLog) + } + ); + } + //----------------------------------------- // extends jquery tmpl to support var def //----------------------------------------- -- 2.39.5