Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#89 Fixing deserialisation of raw message parsed response #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.microtripit.mandrillapp.lutung.view.MandrillHeaderValue;
import com.microtripit.mandrillapp.lutung.view.MandrillMessage;

import java.lang.reflect.Type;
Expand Down Expand Up @@ -45,7 +46,9 @@ public static final GsonBuilder createGsonBuilder() {
.registerTypeAdapter(Date.class, new DateDeserializer())
.registerTypeAdapter(Map.class, new MapSerializer())
.registerTypeAdapter(MandrillMessage.Recipient.Type.class,
new RecipientTypeSerializer());
new RecipientTypeSerializer())
.registerTypeAdapter(MandrillHeaderValue.class,
new MandrillHeaderValueDeserializer());
}

public static final class DateDeserializer
Expand Down Expand Up @@ -134,4 +137,26 @@ public JsonPrimitive serialize(
return new JsonPrimitive(src.name().toLowerCase());
}
}

public static class MandrillHeaderValueDeserializer
implements JsonDeserializer<MandrillHeaderValue> {

@Override
public MandrillHeaderValue deserialize(
final JsonElement jsonElement,
final Type type,
final JsonDeserializationContext context)
throws JsonParseException {

if (jsonElement.isJsonArray()) {
return new MandrillHeaderValue(
(String[]) context.deserialize(jsonElement, String[].class));
}
else {
return new MandrillHeaderValue(jsonElement.getAsString());
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.microtripit.mandrillapp.lutung.view;

import java.util.Arrays;
import java.util.List;

public class MandrillHeaderValue {

private List<String> values;

public MandrillHeaderValue(List<String> values) {
this.values = values;
}

public MandrillHeaderValue(String... values) {
this.values = Arrays.asList(values);
}

public List<String> getValues() {
return values;
}

public String getFirstValue() {
if (values == null || values.isEmpty()) {
return null;
}

return values.get(0);
}

public void setValues(List<String> values) {
this.values = values;
}

public void setValues(String... value) {
this.values = Arrays.asList(value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.microtripit.mandrillapp.lutung.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -15,7 +16,7 @@
public class MandrillMessage {
private String subject, html, text, from_email, from_name;
private List<Recipient> to;
private Map<String,String> headers;
private Map<String,MandrillHeaderValue> headers;
private Boolean important, track_opens, track_clicks, auto_text, auto_html,
inline_css, url_strip_qs, preserve_recipients, view_content_link;
private String bcc_address, tracking_domain, signing_domain,
Expand Down Expand Up @@ -120,11 +121,30 @@ public void setTo(final List<Recipient> to) {
}

/**
* Returns the headers. If a header has multople values
* the first value is returned. If you require both
* values see {@link #getHeadersWithMultipleValuesSupport}
*
* @return Optional extra headers to add to the
* message (currently only Reply-To and X-* headers
* are allowed).
*/
public Map<String,String> getHeaders() {
Map<String, String> firstValueHeaders = new HashMap<String, String>();

for (String key : headers.keySet()) {
firstValueHeaders.put(key, headers.get(key).getFirstValue());
}

return firstValueHeaders;
}

/**
* @return Optional extra headers to add to the
* message (currently only Reply-To and X-* headers
* are allowed).
*/
public Map<String,MandrillHeaderValue> getHeadersWithMultipleValuesSupport() {
return headers;
}

Expand All @@ -134,6 +154,21 @@ public Map<String,String> getHeaders() {
* are allowed)
*/
public void setHeaders(final Map<String,String> headers) {
Map<String, MandrillHeaderValue> multiValueHeaders = new HashMap<String, MandrillHeaderValue>();

for (String key : headers.keySet()) {
multiValueHeaders.put(key, new MandrillHeaderValue());
}

setHeadersWithMultipleValuesSupport(multiValueHeaders);
}

/**
* @param headers Optional extra headers to add to the
* message (currently only Reply-To and X-* headers
* are allowed)
*/
public void setHeadersWithMultipleValuesSupport(final Map<String,MandrillHeaderValue> headers) {
this.headers = headers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ public void testParse02() throws IOException, MandrillApiError {
MandrillMessage parsedMessage = mandrillApi.messages().parse(null);
Assert.fail();
}

@Test
public void testParse03_mutlipleValuesForSingleHeader() throws IOException, MandrillApiError {
String testUnparsedMsg =
"Return-Path: <[email protected]>\n" +
"Received: from mail47.google.com\n" +
"Received: by mail47.google.com with SMTP id 123\n" +
"To: " + mailToAddress()+ "\n" +
"Subject: Lutung test subject\n\n" +
"Sup mandrill !";
MandrillMessage parsedMessage = mandrillApi.messages().parse(testUnparsedMsg);
Assume.assumeNotNull(parsedMessage);
Assert.assertEquals("Lutung test subject", parsedMessage.getSubject());
}

@Test
public void testSmtpInfoResponse() {
Expand Down