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     */
017    package org.apache.servicemix.console;
018    
019    import org.apache.servicemix.JbiConstants;
020    import org.apache.servicemix.jbi.audit.AuditorMBean;
021    import org.apache.servicemix.jbi.jaxp.SourceTransformer;
022    import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
023    
024    import javax.jbi.messaging.MessageExchange;
025    import javax.jbi.messaging.NormalizedMessage;
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.RenderRequest;
029    
030    import java.net.URI;
031    import java.text.DateFormat;
032    import java.util.Calendar;
033    import java.util.Date;
034    
035    public class JBIAuditPortlet extends ServiceMixPortlet {
036        
037        protected int page = 0;
038    
039        public static class ExchangeInfo {
040            private String id;
041            private String date;
042            private String mep;
043            private String status;
044            
045            /**
046             * @return Returns the dateStamp.
047             */
048            public String getDate() {
049                return date;
050            }
051            /**
052             * @param dateStamp The dateStamp to set.
053             */
054            public void setDate(String dateStamp) {
055                this.date = dateStamp;
056            }
057            /**
058             * @return Returns the status.
059             */
060            public String getStatus() {
061                return status;
062            }
063            /**
064             * @param status The status to set.
065             */
066            public void setStatus(String status) {
067                this.status = status;
068            }
069            /**
070             * @return Returns the id.
071             */
072            public String getId() {
073                return id;
074            }
075            /**
076             * @param id The id to set.
077             */
078            public void setId(String id) {
079                this.id = id;
080            }
081            /**
082             * @return Returns the mep.
083             */
084            public String getMep() {
085                return mep;
086            }
087            /**
088             * @param mep The mep to set.
089             */
090            public void setMep(String mep) {
091                this.mep = mep;
092            }
093        }
094        
095        protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
096            System.err.println(actionRequest.getParameterMap());
097            if (actionRequest.getParameter("view") != null) {
098                page = Integer.parseInt(actionRequest.getParameter("view"));
099            }
100        }
101    
102        protected void fillViewRequest(RenderRequest request) throws Exception {
103            AuditorMBean auditor = getJdbcAuditor();
104            int count = auditor.getExchangeCount();
105            request.setAttribute("count", new Integer(count));
106            request.setAttribute("page", new Integer(page));
107            MessageExchange[] exchanges = auditor.getExchangesByRange(page * 10, Math.min((page + 1) * 10, count));
108            request.setAttribute("exchanges", prepare(exchanges));
109            super.fillViewRequest(request);
110        }
111    
112        private ExchangeInfo[] prepare(MessageExchange[] exchanges) throws Exception {
113            ExchangeInfo[] infos = new ExchangeInfo[exchanges.length];
114            for (int i = 0; i < infos.length; i++) {
115                infos[i] = new ExchangeInfo();
116                infos[i].id = exchanges[i].getExchangeId();
117                infos[i].mep = getMep(exchanges[i]);
118                infos[i].status = exchanges[i].getStatus().toString();
119                Object c = exchanges[i].getProperty(JbiConstants.DATESTAMP_PROPERTY_NAME);
120                if (c instanceof Calendar) {
121                    infos[i].date = DateFormat.getDateTimeInstance().format(((Calendar) c).getTime());
122                } else if (c instanceof Date) {
123                    infos[i].date = DateFormat.getDateTimeInstance().format((Date) c);
124                }
125            }
126            return infos;
127        }
128        
129        private String getMep(MessageExchange exchange) {
130            URI uri = exchange.getPattern();
131            if (MessageExchangeSupport.IN_ONLY.equals(uri)) {
132                return "In Only";
133            } else if (MessageExchangeSupport.IN_OPTIONAL_OUT.equals(uri)) {
134                return "In Opt Out";
135            } else if (MessageExchangeSupport.IN_OUT.equals(uri)) {
136                return "In Out";
137            } else if (MessageExchangeSupport.ROBUST_IN_ONLY.equals(uri)) {
138                return "Robust In Only";
139            } else {
140                return uri.toString();
141            }
142        }
143    
144        private String prepareContent(NormalizedMessage msg) throws Exception {
145            if (msg != null) {
146                SourceTransformer st = new SourceTransformer();
147                String s = st.contentToString(msg);
148                if (s != null && s.length() > 30) {
149                    return s.substring(0, 30) + "...";
150                } else {
151                    return s;
152                }
153            } else {
154                return null;
155            }
156        }
157    
158    }