001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.config;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import com.google.common.collect.Maps;
024    import org.apache.commons.codec.binary.Base64;
025    import org.apache.commons.io.IOUtils;
026    import org.apache.commons.lang.StringUtils;
027    import org.sonar.api.utils.DateUtils;
028    
029    import javax.annotation.Nullable;
030    import java.io.IOException;
031    import java.io.StringReader;
032    import java.util.Calendar;
033    import java.util.Date;
034    import java.util.List;
035    import java.util.Map;
036    
037    /**
038     * SonarSource license. This class aims to extract metadata but not to validate or - of course -
039     * to generate license
040     *
041     * @since 3.0
042     */
043    public final class License {
044      private String product;
045      private String organization;
046      private String expirationDate;
047      private String type;
048      private String server;
049    
050      private License(Map<String, String> properties) {
051        product = StringUtils.defaultString(properties.get("Product"), properties.get("Plugin"));
052        organization = StringUtils.defaultString(properties.get("Organisation"), properties.get("Name"));
053        expirationDate = StringUtils.defaultString(properties.get("Expiration"), properties.get("Expires"));
054        type = properties.get("Type");
055        server = properties.get("Server");
056      }
057    
058      @Nullable
059      public String getProduct() {
060        return product;
061      }
062    
063      @Nullable
064      public String getOrganization() {
065        return organization;
066      }
067    
068      @Nullable
069      public String getExpirationDateAsString() {
070        return expirationDate;
071      }
072    
073      @Nullable
074      public Date getExpirationDate() {
075        return DateUtils.parseDateQuietly(expirationDate);
076      }
077    
078      public boolean isExpired() {
079        return isExpired(new Date());
080      }
081    
082      @VisibleForTesting
083      boolean isExpired(Date now) {
084        Date date = getExpirationDate();
085        return date != null && !date.after(org.apache.commons.lang.time.DateUtils.truncate(now, Calendar.DATE));
086      }
087    
088      @Nullable
089      public String getType() {
090        return type;
091      }
092    
093      @Nullable
094      public String getServer() {
095        return server;
096      }
097    
098      public static License readBase64(String base64) {
099        return readPlainText(new String(Base64.decodeBase64(base64.getBytes())));
100      }
101    
102      @VisibleForTesting
103      static License readPlainText(String data) {
104        Map<String, String> props = Maps.newHashMap();
105        StringReader reader = new StringReader(data);
106        try {
107          List<String> lines = IOUtils.readLines(reader);
108          for (String line : lines) {
109            if (StringUtils.isNotBlank(line) && line.indexOf(':') > 0) {
110              String key = StringUtils.substringBefore(line, ":");
111              String value = StringUtils.substringAfter(line, ":");
112              props.put(StringUtils.trimToEmpty(key), StringUtils.trimToEmpty(value));
113            }
114          }
115    
116        } catch (IOException e) {
117          // silently ignore
118    
119        } finally {
120          IOUtils.closeQuietly(reader);
121        }
122        return new License(props);
123      }
124    }