Timestamp variable changes after 1 line of code - java

I am trying to create a dynamic md5 hash system for authenticating to the Marvel API.
I create a timestamp variable, add the private / public key and then try and turn that into a md5 hash. When I print out the results, the timestamp in the url and the timestamp in the string to be hashed becomes different.
//I declare the timestamp here
Long tsLong = System.currentTimeMillis()/1000;
final String ts = tsLong.toString();
//Create the string to be hashed
final String toHash = ts + privateKey + publicKey;
url = "" + publicKey;
String hash = new String();
//my hashing algorithm
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hashe = md.digest(toHash.getBytes("UTF-8"));
StringBuffer hex = new StringBuffer(2*hashe.length);
for (byte b : hashe) {
hex.append(String.format("%02x", b&0xff));
}
hash = hex.toString();
}
catch(NoSuchAlgorithmException e) {
}
catch(UnsupportedEncodingException e) {
}
//append the url to include the hash
url += "&hash=";
url += hash;
//call the api
RequestQueue queue = VolleySingleton.getInstance().getRequestQueue();
JsonObjectRequest obj = new JsonObjectRequest
(Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
parseJSONResponse(response);
for (int i = 0; i < eventArray.size(); i++) {
mTV.append("Id: " + String.valueOf(eventArray.get(i).id) + '\n' +
"Title: " + eventArray.get(i).title + '\n' +
"Description: " + eventArray.get(i).description + '\n');
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//i get an authentication error so here is the log code
Log.i("tag", url);
Log.i("tag", toHash);
}
});
queue.add(obj);
Here is the logcat output
02-13 15:00:10.036 5681-5681/kim.albert.marveleventtracker I/tag﹕ http://gateway.marvel.com/v1/public/events?limit=66&ts=1423538706&apikey=fc57c9d09a46a0bdb06bd811ec4fc078&hash=0a670ef8de3c08d7c3d9dd1bc20aee75
02-13 15:00:10.036 5681-5681/kim.albert.marveleventtracker I/tag﹕ 1423857609172469841247e9a7aa64c3c89771a81299a67ef4fc57c9d09a46a0bdb06bd811ec4fc078
So as you can see: the url ts and the toHash have different values. Why is this?

I am a fool. I was hard coding the ts into my url, instead of updating it by adding the generated ts.

Related

what in kafka DefaultRecord value field

Recently, I review the kafka code and test. I found a strange case:
I print the bytebuffer on the entry of SocketServer processCompletedReceives, as well as print the value on the point of Log sotre as follows:
the entry of SocketServer
private def processCompletedReceives() {
selector.completedReceives.asScala.foreach { receive =>
try {
openOrClosingChannel(receive.source) match {
case Some(channel) =>
val header = RequestHeader.parse(receive.payload)
val connectionId = receive.source
val context = new RequestContext(header, connectionId, channel.socketAddress,
channel.principal, listenerName, securityProtocol)
val req = new RequestChannel.Request(processor = id, context = context,
startTimeNanos = time.nanoseconds, memoryPool, receive.payload, requestChannel.metrics)
if(header.apiKey() == ApiKeys.PRODUCE){
LogHelper.log("produce request: %v" + java.util.Arrays.toString(receive.payload.array()))
}
...
the point of Log
validRecords.records().asScala.foreach { record =>
LogHelper.log("buffer info: value " + java.util.Arrays.toString(record.value().array()))
}
but, the result of print is different. and record.value() is not what I passed in client value like this:
public void run() {
int messageNo = 1;
while (true) {
String messageStr = "Message_" + messageNo;
long startTime = System.currentTimeMillis();
if (isAsync) { // Send asynchronously
producer.send(new ProducerRecord<>(topic,
messageNo,
messageStr), new DemoCallBack(startTime, messageNo, messageStr));
} else { // Send synchronously
try {
producer.send(new ProducerRecord<>(topic,
messageNo,
messageStr)).get();
System.out.println("Sent message: (" + messageNo + ", " + messageStr + ")");
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
++messageNo;
}
}
the print result is not the not String messageStr = "Message_" + messageNo;
so what happend in the case.
done. I write the code as follows:
public class KVExtractor {
private static final Logger logger = LoggerFactory.getLogger(KVExtractor.class);
public static Map.Entry<byte[], byte[]> extract(Record record) {
if (record.hasKey() && record.hasValue()) {
byte[] key = new byte[record.key().limit()];
record.key().get(key);
byte[] value = new byte[record.value().limit()];
record.value().get(value);
System.out.println("key : " + new String(key) + " value: " + new String(value));
return new AbstractMap.SimpleEntry<byte[], byte[]>(key, value);
}else if(record.hasValue()){
// illegal impl
byte[] data = new byte[record.value().limit()];
record.value().get(data);
System.out.println("no key but with value : " + new String(data));
}
return null;
}
}

Using 2 QueryParams to return an object requiring 2 different variables

I'm writing a special permission forms program using MySQL, Javascript, and HTML code, which both respond to. I'm doing all of this using singleton pattern access and facade code(s) in java, and a services code, which responds to the singleton pattern codes.
I'm trying To retrieve a form (AKA 1 Result) by the variables, courseDept & courseNm.
This is a snippet of code I'm using to do all this:
FormDataFacade.java code snippet:
#Path("/specialPermissions/sp")
#GET
#Produces("text/plain")
public Response getSpecialPermissionFormByDeptAndRoomNm(#QueryParam("courseDept") String theDept, #QueryParam("courseNm") String theNm)
throws NamingException, SQLException, ClassNotFoundException
{
//Referenciation to FormDataFacade class.
FormDataFacade iFacade = FormDataFacade.getInstance();
int intNm = 0;
try {
intNm = Integer.parseInt(theNm);
}catch (NumberFormatException FAIL) {
intNm = 1;
}
//Aiming for forms with matching departments & room numbers by calling FormDataFacade
//method, getSpecialPermissionFormByDeptAndRoomNm.
SpecialPermissionForms[] orgaForm = iFacade.getSpecialPermissionFormByDeptAndRoomNm(theDept, intNm);
//Json String Representation...
if (orgaForm != null)
{
Gson neoArcadia = new Gson();
String result = neoArcadia.toJson(orgaForm);
//Json String added to response message body...
ResponseBuilder rb = Response.ok(result, MediaType.TEXT_PLAIN);
rb.status(200); //HTTP Status code has been set!
return rb.build(); //Creating & Returning Response.
}
else
{ //In case of finding no form data for the procedure...
return Response.status(700).build();
}
}
FormDataServices.java code snippet:
public SpecialPermissionForms[] getSpecialPermissionFormByDeptAndRoomNm(String theDept, int theNm) throws SQLException, ClassNotFoundException
{
Connection con = zeon.getConnection();
PreparedStatement pstmt = con.prepareStatement("SELECT formID, studentName, courseDept, courseNm, semester, year, reasonCode FROM spforms WHERE courseDept = ? & courseNm = ?");
pstmt.setString(1, theDept);
pstmt.setInt(2, theNm);
ResultSet rs = pstmt.executeQuery();
SpecialPermissionForms[] neoForm = new SpecialPermissionForms[50];
int current = 0;
while (rs.next())
{
int formID3 = rs.getInt("formID");
String studentName3 = rs.getString("studentName");
String courseDept3 = rs.getString("courseDept");
String courseNm3 = rs.getString("courseNm");
String semester3 = rs.getString("semester");
int year3 = rs.getInt("year");
String reasonCode3 = rs.getString("reasonCode");
SpecialPermissionForms neo = new SpecialPermissionForms(formID3, studentName3, courseDept3, courseNm3, semester3, year3, reasonCode3);
neoForm[current] = neo;
current++;
}
if (current > 0)
{
neoForm = Arrays.copyOf(neoForm, current);
return neoForm;
}
else
{
return null;
}
}
Forms.html code snippet which both pieces of java code respond to:
$("#btnOneName").click(function() {
alert("clicked");
var inputId1=document.getElementById("t_specialFormCourseDept").value;
var inputId2=document.getElementById("t_specialFormCourseNm").value;
var theData = "courseDept=" + inputId1 + "&" + "courseNm=" + inputId2;
alert("Sending: " + theData);
var theUrl = "http://localhost:8080/onlineforms/services/enrollment/specialPermissions/sp?courseDept=&courseNm="+ theData;
$.ajax( {
url: theUrl,
type: "GET",
dataType: "text",
success: function(result) {
alert("success");
var neoForm = JSON.parse(result);
alert(neoForm);
var output="<h3>Current Form Lists:</h3>";
output += "<ul>";
for (var current = 0; current < neoForm.length; current++)
{
output += "<li>" + neoForm[current].formID + ": " + neoForm[current].studentName + " (" + neoForm[current].courseDept + " - " + neoForm[current].courseNm + ") " +
" (" + neoForm[current].semester + " - " + neoForm[current].year + ") " + " - " + neoForm[current].reasonCode + "</li>";
}
output += "</ul>";
alert(output);
$("#p_retrieveOneName").html(output);
},
error:function(xhr) {
alert("error");
$("#p_retrieveOneName").html("Error:"+xhr.status+" "+xhr.statusText);}
} );
});
});
Now, when I go test this code in my webservice after successfully compiling it, it does work, however it retrieves everything, including the specific results I was searching for - I only want to return the results I have specifically searched for and nothing else. What exactly am I doing wrong here in these snippets of code?
Any suggestions or steps in the right direction are highly welcome.

Simplest Java example retrieving user_timeline with twitter API version 1.1

I was looking for a simple Java example using the Twitter 1.1 API and couldn't find one. Using the PHP sample posted here: Simplest PHP example for retrieving user_timeline with Twitter API version 1.1 and a few other Stackoverflow posts, I was able to come up with the following working example.
public void testUserTimelineWithAuthSample() throws Exception {
//This will read the timeline of your account.
String method = "GET";
String url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
String oAuthConsumerKey = "Your value here.";
String oAuthConsumerSecret = "Your value here."; //<--- DO NOT SHARE THIS VALUE
String oAuthAccessToken = "Your value here.";
String oAuthAccessTokenSecret = "Your value here."; //<--- DO NOT SHARE THIS VALUE
String oAuthNonce = String.valueOf(System.currentTimeMillis());
String oAuthSignatureMethod = "HMAC-SHA1";
String oAuthTimestamp = time();
String oAuthVersion = "1.0";
String signatureBaseString1 = method;
String signatureBaseString2 = url;
String signatureBaseString3Templ = "oauth_consumer_key=%s&oauth_nonce=%s&oauth_signature_method=%s&oauth_timestamp=%s&oauth_token=%s&oauth_version=%s";
String signatureBaseString3 = String.format(signatureBaseString3Templ,
oAuthConsumerKey,
oAuthNonce,
oAuthSignatureMethod,
oAuthTimestamp,
oAuthAccessToken,
oAuthVersion);
String signatureBaseStringTemplate = "%s&%s&%s";
String signatureBaseString = String.format(signatureBaseStringTemplate,
URLEncoder.encode(signatureBaseString1, "UTF-8"),
URLEncoder.encode(signatureBaseString2, "UTF-8"),
URLEncoder.encode(signatureBaseString3, "UTF-8"));
System.out.println("signatureBaseString: "+signatureBaseString);
String compositeKey = URLEncoder.encode(oAuthConsumerSecret, "UTF-8") + "&" + URLEncoder.encode(oAuthAccessTokenSecret, "UTF-8");
String oAuthSignature = computeSignature(signatureBaseString, compositeKey);
System.out.println("oAuthSignature : "+oAuthSignature);
String oAuthSignatureEncoded = URLEncoder.encode(oAuthSignature, "UTF-8");
System.out.println("oAuthSignatureEncoded: "+oAuthSignatureEncoded);
String authorizationHeaderValueTempl = "OAuth oauth_consumer_key=\"%s\", oauth_nonce=\"%s\", oauth_signature=\"%s\", oauth_signature_method=\"%s\", oauth_timestamp=\"%s\", oauth_token=\"%s\", oauth_version=\"%s\"";
String authorizationHeaderValue = String.format(authorizationHeaderValueTempl,
oAuthConsumerKey,
oAuthNonce,
oAuthSignatureEncoded,
oAuthSignatureMethod,
oAuthTimestamp,
oAuthAccessToken,
oAuthVersion);
System.out.println("authorizationHeaderValue: "+authorizationHeaderValue);
System.out.println("url: "+url);
System.out.println("authorizationHeaderValue:"+authorizationHeaderValue);
GetMethod getMethod = new GetMethod(url);
getMethod.addRequestHeader("Authorization", authorizationHeaderValue);
HttpClient cli = new HttpClient();
int status = cli.executeMethod(getMethod);
System.out.println("Status:"+status);
long responseContentLength = getMethod.getResponseContentLength();
System.out.println("responseContentLength:"+responseContentLength);
String response = getMethod.getResponseBodyAsString();
System.out.println("response: "+response);
}
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException, Exception
{
SecretKey secretKey = null;
byte[] keyBytes = keyString.getBytes();
secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secretKey);
byte[] text = baseString.getBytes();
return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
}
private String time() {
long millis = System.currentTimeMillis();
long secs = millis / 1000;
return String.valueOf( secs );
}
However, if I add parameters to the url like:
String url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2";
I get:
response: {"errors":[{"message":"Could not authenticate you","code":32}]}
Any idea where this is going wrong?
This works excellent for Timeline with the new Twitter API 1.1
1) Download twitter4j-core-3.0.3.jar in http://twitter4j.org/en/
2) Try use this code:
private static final String TWITTER_CONSUMER_KEY = "xxxxxxxxxxxxxxxxxx";
private static final String TWITTER_SECRET_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private static final String TWITTER_ACCESS_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxx";
private static final String TWITTER_ACCESS_TOKEN_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxx";
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_SECRET_KEY)
.setOAuthAccessToken(TWITTER_ACCESS_TOKEN)
.setOAuthAccessTokenSecret(TWITTER_ACCESS_TOKEN_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
Query query = new Query("MrEdPanama");
QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("#" + tweet.getUser().getScreenName() + " - " + tweet.getText());
}
} while ((query = result.nextQuery()) != null);
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
You are wrong with the oauth_nonce. It is a random 32 bytes string encoded in base 64.
You can build them like this :
public String generateNonce() {
Random gen = new Random(System.currentTimeMillis());
StringBuilder nonceBuilder = new StringBuilder("");
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int baseLength = base.length();
// Taking random word characters
for (int i = 0; i < 32; ++i) {
int position = gen.nextInt(baseLength);
nonceBuilder.append(base.charAt(position));
}
String nonce = toBase64(nonceBuilder.toString());
return nonce;
}
// In your code :
String oAuthNonce = generateNonce();
With String toBase64(String); which is a method to encode a String with Base 64.
Here's a Twitter 1.1 API example that works with parameters. The issue was not related to the nonce. It was the signatureBaseString. Think of the signatureBaseString as a 3 part string delimited by the ampersand (METHOD&URL&PARAMS). The api parameters are NOT to be included in the 2nd part of the signatureBaseString, they are to be included (with the other 6 security parameters) in the last part of signatureBaseString (Also, those params must be in alphabetic order).
public void testUserTimelineWithParams() throws Exception {
//This will read the timeline of the 'twitterapi' account.
String method = "GET";
String url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
List<NameValuePair> urlParams = new ArrayList<NameValuePair>();
urlParams.add( new NameValuePair("screen_name","twitterapi") );
urlParams.add( new NameValuePair("count", "10") );
String oAuthConsumerKey = "Your value";
String oAuthConsumerSecret = "Your value"; //<--- DO NOT SHARE THIS VALUE
String oAuthAccessToken = "Your value";
String oAuthAccessTokenSecret = "Your value"; //<--DO NOT SHARE THIS VALUE
String oAuthNonce = String.valueOf(System.currentTimeMillis());
String oAuthSignatureMethod = "HMAC-SHA1";
String oAuthTimestamp = time();
String oAuthVersion = "1.0";
String signatureBaseString1 = method;
String signatureBaseString2 = url;
List<NameValuePair> allParams = new ArrayList<NameValuePair>();
allParams.add(new NameValuePair("oauth_consumer_key", oAuthConsumerKey));
allParams.add(new NameValuePair("oauth_nonce", oAuthNonce));
allParams.add(new NameValuePair("oauth_signature_method", oAuthSignatureMethod));
allParams.add(new NameValuePair("oauth_timestamp", oAuthTimestamp));
allParams.add(new NameValuePair("oauth_token", oAuthAccessToken));
allParams.add(new NameValuePair("oauth_version", oAuthVersion));
allParams.addAll(urlParams);
Collections.sort(allParams, new NvpComparator());
StringBuffer signatureBaseString3 = new StringBuffer();
for(int i=0;i<allParams.size();i++)
{
NameValuePair nvp = allParams.get(i);
if (i>0) {
signatureBaseString3.append("&");
}
signatureBaseString3.append(nvp.getName() + "=" + nvp.getValue());
}
String signatureBaseStringTemplate = "%s&%s&%s";
String signatureBaseString = String.format(signatureBaseStringTemplate,
URLEncoder.encode(signatureBaseString1, "UTF-8"),
URLEncoder.encode(signatureBaseString2, "UTF-8"),
URLEncoder.encode(signatureBaseString3.toString(), "UTF-8"));
System.out.println("signatureBaseString: "+signatureBaseString);
String compositeKey = URLEncoder.encode(oAuthConsumerSecret, "UTF-8") + "&" + URLEncoder.encode(oAuthAccessTokenSecret, "UTF-8");
String oAuthSignature = computeSignature(signatureBaseString, compositeKey);
System.out.println("oAuthSignature : "+oAuthSignature);
String oAuthSignatureEncoded = URLEncoder.encode(oAuthSignature, "UTF-8");
System.out.println("oAuthSignatureEncoded: "+oAuthSignatureEncoded);
String authorizationHeaderValueTempl = "OAuth oauth_consumer_key=\"%s\", oauth_nonce=\"%s\", oauth_signature=\"%s\", oauth_signature_method=\"%s\", oauth_timestamp=\"%s\", oauth_token=\"%s\", oauth_version=\"%s\"";
String authorizationHeaderValue = String.format(authorizationHeaderValueTempl,
oAuthConsumerKey,
oAuthNonce,
oAuthSignatureEncoded,
oAuthSignatureMethod,
oAuthTimestamp,
oAuthAccessToken,
oAuthVersion);
System.out.println("authorizationHeaderValue: "+authorizationHeaderValue);
StringBuffer urlWithParams = new StringBuffer(url);
for(int i=0;i<urlParams.size();i++) {
if(i==0)
{
urlWithParams.append("?");
}
else
{
urlWithParams.append("&");
}
NameValuePair urlParam = urlParams.get(i);
urlWithParams.append(urlParam.getName() + "=" + urlParam.getValue());
}
System.out.println("urlWithParams: "+urlWithParams.toString());
System.out.println("authorizationHeaderValue:"+authorizationHeaderValue);
GetMethod getMethod = new GetMethod(urlWithParams.toString());
getMethod.addRequestHeader("Authorization", authorizationHeaderValue);
HttpClient cli = new HttpClient();
int status = cli.executeMethod(getMethod);
System.out.println("Status:"+status);
long responseContentLength = getMethod.getResponseContentLength();
System.out.println("responseContentLength:"+responseContentLength);
String response = getMethod.getResponseBodyAsString();
System.out.println("response: "+response);
}
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException, Exception
{
SecretKey secretKey = null;
byte[] keyBytes = keyString.getBytes();
secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secretKey);
byte[] text = baseString.getBytes();
return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
}
private String time() {
long millis = System.currentTimeMillis();
long secs = millis / 1000;
return String.valueOf( secs );
}
Where the NvpComparator is:
public class NvpComparator implements Comparator<NameValuePair> {
public int compare(NameValuePair arg0, NameValuePair arg1) {
String name0 = arg0.getName();
String name1 = arg1.getName();
return name0.compareTo(name1);
}
}
Here is my solution using twitter4j lib
Twitter twitter = new TwitterFactory().getInstance();
AccessToken accessToken = new AccessToken(accessTokenStr, accessTokenSecretStr);
twitter.setOAuthConsumer(consumerKeyStr, consumerSecretStr);
twitter.setOAuthAccessToken(accessToken);
try {
Query query = new Query("#<HASHTAG TO SEARCH>");
QueryResult result;
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("#" + tweet.getUser().getScreenName() + " - " + tweet.getText());
}
}
catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}

how to send attachment using sendgrid mail api

I need to send a mail with attachment in java using sendgrid api. I am mentioning files like [filename.xls]=bytearray. I am getting this byte array using jxl api. I am able to send the mail ,but xls is not having the values.
Code :
sendgridUrl+"?api_user="+apiUser + "&api_key=" + apiPassword + ""
+ to + "&subject=" + (subject.replaceAll(" ", "%20")) + "&fromname="
+ senderPersonal + "&files[" + fileName + "]=" + value2
+ "&html=" + mess‌​age + "&from=" + AppConfig.getProperty("senderemail");
private static bool SendMailUsingSendgrid(EmailSendRequestDTO request)
{
try
{
string htmlContent = string.Empty;
string plainTextContent = string.Empty;
// Set apikey
var apiKey = ConfigReader.Read(CommonEnums.ConfigKeys.SendgridApiKey);
// Initialize sendgrid client.
var client = new SendGridClient(apiKey);
// From address
var from = new EmailAddress(request.FromAddress, request.FromDisplayName);
// Subject
var subject = request.Subject;
// Send mail to multiple recepients.
var recepientList = new List<EmailAddress>();
if (!string.IsNullOrEmpty(request.ToAddresses))
{
foreach (var toAddress in request.ToAddresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
recepientList.Add(new EmailAddress(toAddress));
}
}
// If content in html format
if (request.IsHtmlFormat)
{
htmlContent = request.Body;
}
else
{
plainTextContent = request.Body;
}
// Creat object to send mail message.
var mailMessage = MailHelper.CreateSingleEmailToMultipleRecipients(from, recepientList, subject, plainTextContent, htmlContent);
// add multiple cc.
var ccList = new List<EmailAddress>();
if (!string.IsNullOrEmpty(request.CCAddresses))
{
foreach (var ccAddress in request.CCAddresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
ccList.Add(new EmailAddress(ccAddress));
}
}
// add multiple bcc.
var bccList = new List<EmailAddress>();
if (!string.IsNullOrEmpty(request.BCCAddresses))
{
foreach (var bccAddress in request.BCCAddresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
bccList.Add(new EmailAddress(bccAddress));
}
}
// Attachments
if (!string.IsNullOrEmpty(request.AttachmentFile))
{
foreach (var attachmentFile in request.AttachmentFile.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
SendGrid.Helpers.Mail.Attachment messageAttachment = new SendGrid.Helpers.Mail.Attachment();
mailMessage.Attachments.Add(messageAttachment);
}
}
var response = client.SendEmailAsync(mailMessage).Result;
return true;
}
catch (Exception)
{
return false;
}
}

Reading an email string from a cookie in java

My application needs to store the users email address in a cookie so that I can pre-populate a login form (username == email address). I set the cookie value in JavaScript. If I read it from JavaScript, I get foo#bar.com. If I look at it in the cookie viewer in Firefox I get foo#bar.com.
When I try to read it on the server-side in Java however, I only get foo.
Do I need to do some sort of encoding/decoding here? If so, how do I do it in a way that can be decoded by both JavaScript and Java?
Thanks in advance!
-Michael
From the javax.servlet.http.Cookie doco for setValue(String):
Assigns a new value to a cookie after
the cookie is created. If you use a
binary value, you may want to use
BASE64 encoding.
With Version 0
cookies, values should not contain
white space, brackets, parentheses,
equals signs, commas, double quotes,
slashes, question marks, at signs,
colons, and semicolons. Empty values
may not behave the same way on all
browsers.
I'm guessing you need to BASE64 encode it on the way in (via JavaScript) and on the way out (via Java)
You need to escape the value part of your cookie.
document.cookie = name + "=" +escape( value )
+ ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" )
+ ( ( path ) ? ";path=" + path : "" )
+ ( ( domain ) ? ";domain=" + domain : "" )
+ ( ( secure ) ? ";secure" : "" );
I have found two solutions to this. Here is the first one.
Add back in padding to Base64 encoded strings. Inspiration for this came from http://fi.am/entry/urlsafe-base64-encodingdecoding-in-two-lines/
In this solution, the JavaScript stays the same (base64 encode everything) and the server side looks like:
public class CookieDecoder {
private static final Log log = LogFactory.getLog(CookieDecoder.class);
/**
* #param cookieValue The value of the cookie to decode
* #return Returns the decoded string
*/
public String decode(String cookieValue) {
if (cookieValue == null || "".equals(cookieValue)) {
return null;
}
if (!cookieValue.endsWith("=")) {
cookieValue = padString(cookieValue);
}
if (log.isDebugEnabled()) {
log.debug("Decoding string: " + cookieValue);
}
Base64 base64 = new Base64();
byte[] encodedBytes = cookieValue.getBytes();
byte[] decodedBytes = base64.decode(encodedBytes);
String result = new String(decodedBytes);
if (log.isDebugEnabled()) {
log.debug("Decoded string to: " + result);
}
return result;
}
private String padString(String value) {
int mod = value.length() % 4;
if (mod <= 0) {
return value;
}
int numEqs = 4 - mod;
if (log.isDebugEnabled()) {
log.debug("Padding value with " + numEqs + " = signs");
}
for (int i = 0; i < numEqs; i++) {
value += "=";
}
return value;
}
}
On the JavaScript side, you just need to make sure you base64 encode the values:
var encodedValue = this.base64.encode(value);
document.cookie = name + "=" + encodedValue +
"; expires=" + this.expires.toGMTString() +
"; path=" + this.path;
The second solution is just to URLEncode the Base64 encoded string. I'm using commons codec to do the encoding here. Java Code:
public class CookieDecoder {
private static final Log log = LogFactory.getLog(CookieDecoder.class);
/**
* #param cookieValue The value of the cookie to decode
* #return Returns the decoded string
*/
public String decode(String cookieValue) {
if (cookieValue == null || "".equals(cookieValue)) {
return null;
}
if (log.isDebugEnabled()) {
log.debug("Decoding string: " + cookieValue);
}
URLCodec urlCodec = new URLCodec();
String b64Str;
try {
b64Str = urlCodec.decode(cookieValue);
}
catch (DecoderException e) {
log.error("Error decoding string: " + cookieValue);
return null;
}
Base64 base64 = new Base64();
byte[] encodedBytes = b64Str.getBytes();
byte[] decodedBytes = base64.decode(encodedBytes);
String result = new String(decodedBytes);
if (log.isDebugEnabled()) {
log.debug("Decoded string to: " + result);
}
return result;
}
}
But now I have to decode it on the JavaScript side as well...
Encode:
var encodedValue = this.base64.encode(value);
document.cookie = name + "=" + escape(encodedValue) +
"; expires=" + this.expires.toGMTString() +
"; path=" + this.path;
Decode:
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
var encodedValue = c.substring(nameEQ.length,c.length);
return this.base64.decode(unescape(encodedValue));
}
}
return null;

Categories

Resources