001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.config;
020
021import java.io.IOException;
022import java.util.Arrays;
023import java.util.HashSet;
024import java.util.Set;
025
026import lucee.commons.digest.MD5;
027import lucee.commons.net.IPRange;
028import lucee.runtime.type.Struct;
029import lucee.runtime.type.util.ListUtil;
030
031public class DebugEntry {
032
033        private final String id;
034        private final String type;
035        private final String strIpRange;
036        private final IPRange ipRange;
037        private final String label;
038        private final Struct custom;
039        private final boolean readOnly;
040        private final String path;
041        private final String fullname;
042
043        public DebugEntry(String id, String type, String ipRange, String label, String path, String fullname, Struct custom) throws IOException {
044                this(id,type,IPRange.getInstance(ipRange),ipRange,label,path,fullname,custom,false);
045        }
046        
047        private DebugEntry(String id, String type, IPRange ipRange,String strIpRange, String label, String path, String fullname, Struct custom, boolean readOnly) {
048                this.id=id;
049                this.type=type;
050                this.strIpRange=strIpRange;
051                this.ipRange=ipRange;
052                this.label=label;
053                this.custom=custom;
054                this.readOnly=readOnly;
055                this.path=path;
056                this.fullname=fullname;
057        }
058        
059        /**
060         * @return the path
061         */
062        public String getPath() {
063                return path;
064        }
065
066        /**
067         * @return the fullname
068         */
069        public String getFullname() {
070                return fullname;
071        }
072
073        /**
074         * @return the readOnly
075         */
076        public boolean isReadOnly() {
077                return readOnly;
078        }
079
080        /**
081         * @return the id
082         */
083        public String getId() {
084                return id;
085        }
086
087        /**
088         * @return the type
089         */
090        public String getType() {
091                return type;
092        }
093
094        /**
095         * @return the ipRange
096         */
097        public String getIpRangeAsString() {
098                return strIpRange;
099        }
100        public IPRange getIpRange() {
101                return ipRange;
102        }
103
104        /**
105         * @return the label
106         */
107        public String getLabel() {
108                return label;
109        }
110
111        /**
112         * @return the custom
113         */
114        public Struct getCustom() {
115                return (Struct) custom.duplicate(false);
116        }
117
118        public DebugEntry duplicate(boolean readOnly) {
119                DebugEntry de = new DebugEntry(id, type, ipRange,strIpRange, label, path,fullname,custom,readOnly);
120                return de;
121        }
122        
123        public static String organizeIPRange(String ipRange) {
124                String[] arr = ListUtil.trim(ListUtil.trimItems(ListUtil.listToStringArray(ipRange, ',')));
125                Set<String> set=new HashSet<String>();
126                for(int i=0;i<arr.length;i++){
127                        set.add(arr[i]);
128                }
129                arr=set.toArray(new String[set.size()]);
130                Arrays.sort(arr);
131                return ListUtil.arrayToList(arr, ",");
132        }
133        
134
135        public static String ipRangeToId(String ipRange) {
136                ipRange=organizeIPRange(ipRange);
137                try {
138                        return MD5.getDigestAsString(ipRange);
139                } catch (IOException e) {
140                        return ipRange;
141                }
142        }
143        
144        /*public static void main(String[] args) {
145                print.o(ipRangeToId("*,127.0.0.1"));
146                print.o(ipRangeToId(",,,*,127.0.0.1"));
147                print.o(ipRangeToId(",,,*,127.0.0.1,*"));
148        }*/
149        
150
151}