Pham Ngoc Hai personal web site

Syndicate

Programming
ASP.NET MVC3 jQuery form POST and Redirect
 
Written by Pham Ngoc Hai, on 26-11-2011 10:18

It has been some time since I touched ASP and M$ stuff. Today while working with ASP.NET and jQueryMobile, I discovered that my redirection no longer worked.

If you have a form POST to an action that return a Redirect like:

[HttpPost]

public ActionResult Reserve(ReserveViewModel reserveViewModel)

{

....

return Redirect(redirectUrl);

 

ISS will return an HTTP 302 redirection, in order for jQuery to redirect to that URL instead of using AJAX to handle. Your form must have an data-ajax="false", like:

<form action="/Ticket/Reserve" data-ajax="false" method="post">

....

</form> 

That will disable AJAX handling on that form and your browser will happily redirect to the new address. 

Last update: 26-11-2011 10:34

Published in : Computer stuff, Programming
User comments Quote this article in website Favoured Print Send to friend Save this to del.icio.us Related articles Read more...

Camel File readLock
 
Written by Pham Ngoc Hai, on 12-01-2011 09:34

When running Camel in ServicemMix on linux, the only reliable readLock the we use is "changed", but if the writer is writing very slowly, the defaut time 1s between checks is not sufficient. In the end, we have to implement a org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy to provide to exclusiveReadLockStrategy, This class is basically the same as FileChangedExclusiveReadLockStrategy but with a longer sleep time.

I have made a patch to provide and option for user to specify the time between checks

the patch is at: https://issues.apache.org/jira/browse/CAMEL-3520 but it's only available in camel 2.6.

For older camel versions, we have to provide a  exclusiveReadLockStrategy as followed:

 


import java.io.File;
import java.io.IOException;

import org.apache.camel.Exchange;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.GenericFileOperations;
import org.apache.camel.component.file.strategy.FileChangedExclusiveReadLockStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class ChangedFileExclusiveReadLockStrategy extends FileChangedExclusiveReadLockStrategy {
   

       private static final transient Log LOG = LogFactory.getLog(ChangedFileExclusiveReadLockStrategy.class);
        private long timeout;

        @Override
        public void prepareOnStartup(GenericFileOperations<File> operations, GenericFileEndpoint<File> endpoint) {
            // noop
        }

        public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
            File target = new File(file.getAbsoluteFilePath());
            boolean exclusive = false;

            if (LOG.isTraceEnabled()) {
                LOG.trace("Waiting for exclusive read lock to file: " + file);
            }

            try {
                long lastModified = Long.MIN_VALUE;
                long length = Long.MIN_VALUE;
                long start = System.currentTimeMillis();
                //StopWatch watch = new StopWatch();

                while (!exclusive) {
                    // timeout check
                    if (timeout > 0) {
                         long delta = System.currentTimeMillis() - start;
                        if (delta > timeout) {
                            LOG.warn("Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file);
                            // we could not get the lock within the timeout period, so return false
                            return false;
                        }
                    }

                    long newLastModified = target.lastModified();
                    long newLength = target.length();

                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified);
                        LOG.trace("Previous length: " + length + ", new length: " + newLength);
                    }

                    if (newLastModified == lastModified && newLength == length) {
                        // let super handle the last part of acquiring the lock now the file is not
                        // currently being in progress of being copied as file length and modified
                        // are stable
                        exclusive = super.acquireExclusiveReadLock(operations, file, exchange);
                    } else {
                        // set new base file change information
                        lastModified = newLastModified;
                        length = newLength;

                        boolean interrupted = sleep();
                        if (interrupted) {
                            // we were interrupted while sleeping, we are likely being shutdown so return false
                            return false;
                        }
                    }
                }
            } catch (IOException e) {
                // must handle IOException as some apps on Windows etc. will still somehow hold a lock to a file
                // such as AntiVirus or MS Office that has special locks for it's supported files
                if (timeout == 0) {
                    // if not using timeout, then we cant retry, so rethrow
                    throw e;
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Cannot acquire read lock. Will try again.", e);
                }
                boolean interrupted = sleep();
                if (interrupted) {
                    // we were interrupted while sleeping, we are likely being shutdown so return false
                    return false;
                }
            }

            return exclusive;
        }

        private boolean sleep() {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Exclusive read lock not granted. Sleeping for 10000 millis.");
            }
            try {
                Thread.sleep(10000);
                return false;
            } catch (InterruptedException e) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Sleep interrupted while waiting for exclusive read lock, so breaking out");
                }
                return true;
            }
        }

        public long getTimeout() {
            return timeout;
        }

        public void setTimeout(long timeout) {
            this.timeout = timeout;
        }
}

 

Last update: 12-01-2011 09:40

Published in : Computer stuff, Programming
User comments Quote this article in website Favoured Print Send to friend Save this to del.icio.us Related articles Read more...

RSA Cipher Block Chaining Mode in Java
 
Written by Pham Ngoc Hai, on 24-12-2007 10:53

This is one of my class assignment in Security, please take a look at my Miller-Rabin prime number test for more information.

Enjoy.

 

Last update: 15-03-2008 09:15

Published in : Computer stuff, Programming
Keywords : RSA, CBC, Cipher Block Chaining Mode, Java
User comments Quote this article in website Favoured Print Send to friend Save this to del.icio.us Related articles Read more...

Miller-Rabin prime test in Java
 
Written by Pham Ngoc Hai, on 27-11-2007 15:11

A simple Miller-Rabin prime number test I wrote in Java.

Along with this method I implemented isWitness as well as the modulo power by bit shifting method.

Feel free to comment ;)

=================

Update: 

Someone requested for myBigRanNum and FactorOfTwo, I put them here as well, enjoy.

=================

 

Last update: 15-03-2008 09:14

Published in : Computer stuff, Programming
Keywords : Computer stuff, Programming, Miller Rabin prime test in JavaMiller Rabin prime number test Java, witness, modulo power, bit shifting
User comments Quote this article in website Favoured Print Send to friend Save this to del.icio.us Related articles Read more...



Search

Calendar

 Jan   February 2012   Mar

SMTWTFS
   1  2  3  4
  5  6  7  8  91011
12131415161718
19202122232425
26272829 
VC Funding Resources

Random Photos






Donate

Enter Amount:

Sponsored Links

Copyright © 2007 Joomla Templates By Joomladesigns.  Modified By Pham Ngoc Hai