View Javadoc
1   /*
2    * Copyright (c) 2023. Roland T. Lichti, Kaiserpfalz EDV-Service.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16   */
17  
18  /*
19   * Libravatar -- Java Library for retrieving libravatars.
20   *
21   * The code in this package is taken from {@linkplain https://github.com/alessandroleite/libravatar-j}.
22   *
23   * It is licensed under a MIT license by Alessandro Leite.
24   */
25  package de.kaiserpfalzedv.commons.core.libravatar;
26  
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.net.URI;
30  import java.nio.charset.Charset;
31  import java.util.Locale;
32  
33  import org.apache.commons.codec.digest.DigestUtils;
34  import org.apache.commons.io.IOUtils;
35  
36  import de.kaiserpfalzedv.commons.api.libravatar.Avatar;
37  import de.kaiserpfalzedv.commons.api.libravatar.AvatarException;
38  import de.kaiserpfalzedv.commons.api.libravatar.AvatarOptions;
39  import de.kaiserpfalzedv.commons.api.libravatar.LibravatarDefaultImage;
40  import jakarta.inject.Inject;
41  import lombok.EqualsAndHashCode;
42  import lombok.Getter;
43  import lombok.RequiredArgsConstructor;
44  import lombok.ToString;
45  import lombok.experimental.Accessors;
46  import lombok.extern.slf4j.Slf4j;
47  
48  @RequiredArgsConstructor(onConstructor = @__(@Inject))
49  @ToString(onlyExplicitlyIncluded = true)
50  @EqualsAndHashCode(onlyExplicitlyIncluded = true)
51  @Accessors(chain = true, fluent = false)
52  @Slf4j
53  public class AvatarImpl implements Avatar {
54      @ToString.Include
55      @EqualsAndHashCode.Include
56      @Getter
57      private final String email;
58  
59      @Override
60      public byte[] download(final de.kaiserpfalzedv.commons.api.libravatar.AvatarOptions options) {
61          try (InputStream is = URI.create(this.buildUrl(options)).toURL().openStream()) {
62              return IOUtils.toByteArray(is);
63          } catch (final IOException e) {
64              throw new AvatarException(e);
65          }
66      }
67  
68      @Override
69      public String buildUrl(final AvatarOptions options) {
70          log.debug("avatar build. https={}, sha256={}, size={}",
71                  options.useHttps(), options.useSHA256(), options.imageSize());
72  
73          final StringBuilder sb = new StringBuilder(
74                  options.useHttps() ? options.secureBaseUri()
75                          : options.baseUri());
76  
77          sb.append(
78                  options.useSHA256() ? DigestUtils.sha256Hex(this.email.toLowerCase(Locale.getDefault()).getBytes(Charset.defaultCharset()))
79                          : DigestUtils.md5Hex(this.email.toLowerCase(Locale.getDefault()).getBytes(Charset.defaultCharset())));
80  
81          sb.append("?s=").append(options.imageSize());
82  
83          sb.append(
84                  options.defaultImage() != LibravatarDefaultImage.DEFAULT ? "&d=" + options.defaultImage().getCode()
85                          : "");
86  
87          return sb.toString();
88      }
89  }