Skip to content

Commit

Permalink
Enable spotless check (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Jul 8, 2024
1 parent a7c70a9 commit 7f4da2e
Show file tree
Hide file tree
Showing 18 changed files with 794 additions and 803 deletions.
25 changes: 12 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
Expand All @@ -12,19 +12,19 @@
<artifactId>gravatar</artifactId>
<version>${changelist}</version>
<packaging>hpi</packaging>

<name>Jenkins Gravatar plugin</name>
<description>This plugin shows Gravatar avatar images for Jenkins users.</description>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>

<licenses>
<license>
<name>MIT</name>
<url>https://opensource.org/license/mit/</url>
<comments>Copyright 2011 Erik Ramfelt. All rights reserved.</comments>
</license>
</licenses>

<developers>
<developer>
<id>redsolo</id>
Expand All @@ -39,7 +39,7 @@
<timezone>+1</timezone>
</developer>
</developers>

<contributors>
<contributor>
<name>David M. Carr</name>
Expand All @@ -52,23 +52,24 @@
<timezone>+1</timezone>
</contributor>
</contributors>

<scm>
<connection>scm:git:https://github.com/${gitHubRepo}.git</connection>
<developerConnection>scm:git:[email protected]:${gitHubRepo}.git</developerConnection>
<url>https://github.com/${gitHubRepo}</url>
<tag>${scmTag}</tag>
<url>https://github.com/${gitHubRepo}</url>
</scm>

<issueManagement>
<system>Github</system>
<url>https://github.com/${gitHubRepo}/issues</url>
</issueManagement>

<properties>
<changelist>999999-SNAPSHOT</changelist>
<jenkins.version>2.440.3</jenkins.version>
<gitHubRepo>jenkinsci/gravatar-plugin</gitHubRepo>
<spotless.check.skip>false</spotless.check.skip>
</properties>

<dependencyManagement>
Expand All @@ -77,8 +78,8 @@
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.440.x</artifactId>
<version>3180.vc1df4d5b_8097</version>
<scope>import</scope>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down Expand Up @@ -131,6 +132,4 @@
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>


</project>
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* The MIT License
*
*
* Copyright (c) 2011, Erik Ramfelt
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -23,12 +23,11 @@
*/
package org.jenkinsci.plugins.gravatar;

import java.util.logging.Logger;

import com.google.common.annotations.VisibleForTesting;
import hudson.Extension;
import hudson.model.User;
import hudson.tasks.UserAvatarResolver;
import java.util.logging.Logger;
import org.jenkinsci.plugins.gravatar.cache.GravatarImageResolutionCache;
import org.jenkinsci.plugins.gravatar.cache.GravatarImageResolutionCacheInstance;
import org.jenkinsci.plugins.gravatar.model.GravatarUrlCreator;
Expand All @@ -39,29 +38,29 @@
@Extension
public class UserGravatarResolver extends UserAvatarResolver {

private static final Logger LOG = Logger.getLogger(UserGravatarResolver.class.getName());
private static final Logger LOG = Logger.getLogger(UserGravatarResolver.class.getName());

@Override
public String findAvatarFor(User user, int width, int height) {
if (isGravatarUser(user)) {
LOG.finest("Resolving gravatar url for user " + user.getId() + " in size " + width + "x" + height);
GravatarUrlCreator urlCreator = urlCreatorFor(user);
return urlCreator.buildUrlForSize(width);
}
return null; //we cannot contribute to the avatar resolution for this user
if (isGravatarUser(user)) {
LOG.finest("Resolving gravatar url for user " + user.getId() + " in size " + width + "x" + height);
GravatarUrlCreator urlCreator = urlCreatorFor(user);
return urlCreator.buildUrlForSize(width);
}
return null; // we cannot contribute to the avatar resolution for this user
}

@VisibleForTesting
protected GravatarUrlCreator urlCreatorFor(User user) {
return cache().urlCreatorFor(user).get();
}
@VisibleForTesting
protected GravatarUrlCreator urlCreatorFor(User user) {
return cache().urlCreatorFor(user).get();
}

boolean isGravatarUser(User user) {
return cache().hasGravatarCreator(user);
boolean isGravatarUser(User user) {
return cache().hasGravatarCreator(user);
}

@VisibleForTesting
GravatarImageResolutionCache cache() {
return GravatarImageResolutionCacheInstance.INSTANCE;
}
@VisibleForTesting
GravatarImageResolutionCache cache() {
return GravatarImageResolutionCacheInstance.INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* The MIT License
*
*
* Copyright (c) 2011, Erik Ramfelt
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -23,6 +23,11 @@
*/
package org.jenkinsci.plugins.gravatar.boundary;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.VisibleForTesting;
import de.bripkens.gravatar.Gravatar;
import hudson.ProxyConfiguration;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
Expand All @@ -32,64 +37,61 @@
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.logging.Logger;

import com.google.common.annotations.VisibleForTesting;
import de.bripkens.gravatar.Gravatar;
import hudson.ProxyConfiguration;
import org.jenkinsci.plugins.gravatar.factory.GravatarFactory;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Class that verifies that a Gravatar exists for an email.
*/
public class GravatarImageURLVerifier {

private static final Logger LOG = Logger.getLogger(GravatarImageURLVerifier.class.getName());

/**
* Verifies if the email has an Gravatar
*
* @param email email address
* @return true, if there is a Gravatar for the emails; false, otherwise.
*/
public boolean verify(String email) {
checkNotNull(email);
String imageURL = gravatar().getUrl(email);
private static final Logger LOG = Logger.getLogger(GravatarImageURLVerifier.class.getName());

boolean gravatarExistsForEmail = false;
try {
URI url = new URI(imageURL);
HttpClient client = ProxyConfiguration.newHttpClientBuilder().followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(5)).build();
HttpRequest request = ProxyConfiguration.newHttpRequestBuilder(url).timeout(Duration.ofSeconds(5))
.method("HEAD", HttpRequest.BodyPublishers.noBody()).build();
/**
* Verifies if the email has an Gravatar
*
* @param email email address
* @return true, if there is a Gravatar for the emails; false, otherwise.
*/
public boolean verify(String email) {
checkNotNull(email);
String imageURL = gravatar().getUrl(email);

HttpResponse<?> resp = client.send(request, HttpResponse.BodyHandlers.discarding());
boolean gravatarExistsForEmail = false;
try {
URI url = new URI(imageURL);
HttpClient client = ProxyConfiguration.newHttpClientBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(5))
.build();
HttpRequest request = ProxyConfiguration.newHttpRequestBuilder(url)
.timeout(Duration.ofSeconds(5))
.method("HEAD", HttpRequest.BodyPublishers.noBody())
.build();

int gravatarResponseCode = resp.statusCode();
gravatarExistsForEmail = responseCodeIsOK(gravatarResponseCode);
HttpResponse<?> resp = client.send(request, HttpResponse.BodyHandlers.discarding());

LOG.finer("Resolved gravatar for " + email + ". Found: " + gravatarExistsForEmail);
} catch (URISyntaxException e) {
LOG.warning("Gravatar URL is malformed, " + imageURL);
} catch (IOException e) {
LOG.fine("Could not connect to the Gravatar URL, " + e);
} catch (InterruptedException e) {
LOG.fine("Could not connect to the Gravatar URL, " + e);
}
return gravatarExistsForEmail;
}
int gravatarResponseCode = resp.statusCode();
gravatarExistsForEmail = responseCodeIsOK(gravatarResponseCode);

@VisibleForTesting
protected Gravatar gravatar() {
return new GravatarFactory().verifyingGravatar();
}
LOG.finer("Resolved gravatar for " + email + ". Found: " + gravatarExistsForEmail);
} catch (URISyntaxException e) {
LOG.warning("Gravatar URL is malformed, " + imageURL);
} catch (IOException e) {
LOG.fine("Could not connect to the Gravatar URL, " + e);
} catch (InterruptedException e) {
LOG.fine("Could not connect to the Gravatar URL, " + e);
}
return gravatarExistsForEmail;
}

private boolean responseCodeIsOK(int gravatarResponseCode) {
System.err.println("gravatarResponseCode: " + gravatarResponseCode);
return (gravatarResponseCode == HttpURLConnection.HTTP_OK)
|| (gravatarResponseCode == HttpURLConnection.HTTP_NOT_MODIFIED);
}
@VisibleForTesting
protected Gravatar gravatar() {
return new GravatarFactory().verifyingGravatar();
}

private boolean responseCodeIsOK(int gravatarResponseCode) {
System.err.println("gravatarResponseCode: " + gravatarResponseCode);
return (gravatarResponseCode == HttpURLConnection.HTTP_OK)
|| (gravatarResponseCode == HttpURLConnection.HTTP_NOT_MODIFIED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

public interface GravatarImageResolutionCache {

Optional<GravatarUrlCreator> urlCreatorFor(User user);
Optional<GravatarUrlCreator> urlCreatorFor(User user);

boolean hasGravatarCreator(User user);
boolean hasGravatarCreator(User user);

void loadIfUnknown(User user);
void loadIfUnknown(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
import org.jenkinsci.plugins.gravatar.model.GravatarUrlCreator;

public enum GravatarImageResolutionCacheInstance implements GravatarImageResolutionCache {
INSTANCE;
INSTANCE;

final GravatarImageResolutionLoadingCache cache = new GravatarImageResolutionLoadingCache();
final GravatarImageResolutionLoadingCache cache = new GravatarImageResolutionLoadingCache();

public Optional<GravatarUrlCreator> urlCreatorFor(User user) {
return cache.urlCreatorFor(user);
}
public Optional<GravatarUrlCreator> urlCreatorFor(User user) {
return cache.urlCreatorFor(user);
}

public void loadIfUnknown(User user) {
cache.loadIfUnknown(user);
}

public boolean hasGravatarCreator(User user) {
return cache.hasGravatarCreator(user);
}
public void loadIfUnknown(User user) {
cache.loadIfUnknown(user);
}

public boolean hasGravatarCreator(User user) {
return cache.hasGravatarCreator(user);
}
}
Loading

0 comments on commit 7f4da2e

Please sign in to comment.