001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.core.request.handler.logger;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.behavior.Behavior;
021import org.apache.wicket.core.request.handler.IPageAndComponentProvider;
022import org.apache.wicket.markup.html.form.Form;
023import org.apache.wicket.markup.html.form.IFormSubmitter;
024import org.apache.wicket.request.component.IRequestableComponent;
025
026/**
027 * Contains logging data for component/listener-interface request handlers.
028 *
029 * @author Emond Papegaaij
030 */
031public class ListenerLogData extends PageLogData
032{
033        private static final long serialVersionUID = 1L;
034
035        private final Class<? extends IRequestableComponent> componentClass;
036        private final String componentPath;
037        private final Integer behaviorIndex;
038        private final Class<? extends Behavior> behaviorClass;
039        private final Class<? extends IRequestableComponent> submittingComponentClass;
040        private final String submittingComponentPath;
041
042        /**
043         * Construct.
044         *
045         * @param pageAndComponentProvider
046         * @param behaviorIndex
047         */
048        public ListenerLogData(IPageAndComponentProvider pageAndComponentProvider, Integer behaviorIndex)
049        {
050                super(pageAndComponentProvider);
051
052                this.behaviorIndex = behaviorIndex;
053
054                componentClass = optional(() -> pageAndComponentProvider.getComponent().getClass());
055                componentPath = optional(() -> pageAndComponentProvider.getComponentPath());
056
057                if (behaviorIndex != null)
058                {
059                        behaviorClass = optional(() -> pageAndComponentProvider.getComponent()
060                                        .getBehaviorById(behaviorIndex)
061                                        .getClass());
062                }
063                else
064                {
065                        behaviorClass = null;
066                }
067                
068                final Component formSubmitter = optional(() -> {
069                        final IRequestableComponent component = pageAndComponentProvider.getComponent();
070                        if (component instanceof Form)
071                        {
072                                final IFormSubmitter submitter = ((Form<?>)component).findSubmitter();
073                                return submitter instanceof Component ? (Component)submitter : null;
074                        }
075                        return null;
076                });
077                if (formSubmitter != null)
078                {
079                        submittingComponentClass = formSubmitter.getClass();
080                        submittingComponentPath = formSubmitter.getPageRelativePath();
081                } else {
082                        submittingComponentClass = null;
083                        submittingComponentPath = null;
084                }
085        }
086
087        /**
088         * @return componentClass
089         */
090        public final Class<? extends IRequestableComponent> getComponentClass()
091        {
092                return componentClass;
093        }
094
095        /**
096         * @return componentPath
097         */
098        public final String getComponentPath()
099        {
100                return componentPath;
101        }
102
103        /**
104         * @return behaviorIndex
105         */
106        public final Integer getBehaviorIndex()
107        {
108                return behaviorIndex;
109        }
110
111        /**
112         * @return behaviorClass
113         */
114        public final Class<? extends Behavior> getBehaviorClass()
115        {
116                return behaviorClass;
117        }
118
119        /**
120         * @return submittingComponentClass
121         */
122        public Class<? extends IRequestableComponent> getSubmittingComponentClass()
123        {
124                return submittingComponentClass;
125        }
126
127        /**
128         * @return submittingComponentPath
129         */
130        public String getSubmittingComponentPath()
131        {
132                return submittingComponentPath;
133        }
134
135        @Override
136        public String toString()
137        {
138                StringBuilder sb = new StringBuilder(super.toString());
139                sb.setCharAt(sb.length() - 1, ',');
140                if (getComponentClass() != null)
141                {
142                        sb.append("componentClass=");
143                        sb.append(getComponentClass().getName());
144                        sb.append(',');
145                }
146                if (getComponentPath() != null)
147                {
148                        sb.append("componentPath=");
149                        sb.append(getComponentPath());
150                        sb.append(',');
151                }
152                sb.append("behaviorIndex=");
153                sb.append(getBehaviorIndex());
154                if (getBehaviorClass() != null)
155                {
156                        sb.append(",behaviorClass=");
157                        sb.append(getBehaviorClass().getName());
158                }
159                if (getSubmittingComponentClass() != null)
160                {
161                        sb.append(",submittingComponentClass=");
162                        sb.append(getSubmittingComponentClass().getName());
163                }
164                if (getSubmittingComponentPath() != null)
165                {
166                        sb.append(",submittingComponentPath=");
167                        sb.append(getSubmittingComponentPath());
168                }
169                sb.append("}");
170                return sb.toString();
171        }
172}