-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from ukby1234/email-custom-headers
add custom headers support for emails
- Loading branch information
Showing
7 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mailgun.form; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Documented | ||
@Target(FIELD) | ||
@Retention(RUNTIME) | ||
public @interface CustomProperties { | ||
|
||
/** | ||
* The name of the property. | ||
*/ | ||
String prefix (); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.mailgun.form; | ||
|
||
import feign.form.FormProperty; | ||
import lombok.NoArgsConstructor; | ||
import lombok.NonNull; | ||
import lombok.Setter; | ||
import lombok.SneakyThrows; | ||
import lombok.experimental.FieldDefaults; | ||
import lombok.val; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Type; | ||
import java.rmi.UnexpectedException; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static java.lang.reflect.Modifier.isFinal; | ||
import static java.lang.reflect.Modifier.isStatic; | ||
import static lombok.AccessLevel.PRIVATE; | ||
|
||
/** | ||
* | ||
* @author Artem Labazin | ||
*/ | ||
public final class PojoUtil { | ||
|
||
public static boolean isUserPojo (@NonNull Object object) { | ||
val type = object.getClass(); | ||
val packageName = type.getPackage().getName(); | ||
return !packageName.startsWith("java."); | ||
} | ||
|
||
public static boolean isUserPojo (@NonNull Type type) { | ||
val typeName = type.toString(); | ||
return !typeName.startsWith("class java."); | ||
} | ||
|
||
@SneakyThrows | ||
public static Map<String, Object> toMap (@NonNull Object object) { | ||
val result = new HashMap<String, Object>(); | ||
val type = object.getClass(); | ||
val setAccessibleAction = new PojoUtil.SetAccessibleAction(); | ||
for (val field : type.getDeclaredFields()) { | ||
val modifiers = field.getModifiers(); | ||
if (isFinal(modifiers) || isStatic(modifiers)) { | ||
continue; | ||
} | ||
setAccessibleAction.setField(field); | ||
AccessController.doPrivileged(setAccessibleAction); | ||
|
||
val fieldValue = field.get(object); | ||
if (fieldValue == null) { | ||
continue; | ||
} | ||
if (field.isAnnotationPresent(CustomProperties.class)) { | ||
String prefix = field.getAnnotation(CustomProperties.class).prefix(); | ||
Map<String, String> properties = (Map<String, String>) fieldValue; | ||
for (Map.Entry<String, String> entry : properties.entrySet()) { | ||
result.put(prefix + entry.getKey(), entry.getValue()); | ||
} | ||
} else { | ||
val propertyKey = field.isAnnotationPresent(FormProperty.class) | ||
? field.getAnnotation(FormProperty.class).value() | ||
: field.getName(); | ||
|
||
result.put(propertyKey, fieldValue); | ||
} | ||
|
||
} | ||
return result; | ||
} | ||
|
||
private PojoUtil () throws UnexpectedException { | ||
throw new UnexpectedException("It is not allowed to instantiate this class"); | ||
} | ||
|
||
@Setter | ||
@NoArgsConstructor | ||
@FieldDefaults(level = PRIVATE) | ||
private static class SetAccessibleAction implements PrivilegedAction<Object> { | ||
|
||
Field field; | ||
|
||
@Override | ||
public Object run () { | ||
field.setAccessible(true); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters