Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 21
Registered: ‎09-25-2009
My Device: 9700
My Carrier: Turkcell

HttpFilterRegistry (Http Filter) is Invoked Only Once

I am using BlackBerry 9000 OS 4.6.0 simulator.

 

I need to open my 3rd party app when user clicked on web url in his/her mail screen.

 

I used http filter for subhost.mytesthost.com.tr FDN.

 

When I clicked to URL first time, my app is invoked. But closed application and mail scren, then re-opened mail and clicked on same URL, temporary browser page is opened but application is not invoked.

 

I think, device is cached temporary page because I debugged the code and openFilter method is not triggered during 2nd request.

Why ? Any body know this problem ?

 

 

/**
 * PackageManager.java
 * 
 */
package com.rim.samples.device.httpfilterdemo;

import net.rim.device.api.io.http.HttpFilterRegistry;
import net.rim.device.api.system.ControlledAccessException;

final class PackageManager
{
	public static void libMain(String[] args) 
	{
		HttpFilterRegistry.registerFilter("subhost.mytesthost.com.tr", "com.rim.samples.device.httpfilterdemo.precanned", false);
	}
}

 

/**
 * Protocol.java
 * 
 */
 
package com.rim.samples.device.httpfilterdemo.precanned;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.io.Connection;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.io.FilterBaseInterface;
import net.rim.device.api.io.http.HttpDateParser;
import net.rim.device.api.io.http.HttpHeaders;
import net.rim.device.api.io.http.HttpProtocolConstants;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.ApplicationManagerException;
import net.rim.device.api.system.CodeModuleManager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Status;

/**
 * An example of the HttpFilterRegistry apis.
 */
public final class Protocol implements FilterBaseInterface, HttpConnection
{
	private String         _url;
	private String         _requestMethod;
	private HttpHeaders    _requestHeaders;
	private HttpHeaders    _responseHeaders;
	private byte[]         _resultData;
	
	private Timer timerTrigger;
	
	/**
	 * This function will open a filtered Http Connection.
	 * 
	 * @see net.rim.device.api.io.FilterBaseInterface#openFilter(String, int, boolean)
	 */
	public Connection openFilter(String name, int mode, boolean timeouts) throws IOException
	{
		_url = name.substring(2);
		
		String staticPageDataString = "<html>" + 
		"<head>" +
		"<title>Lotus Mobil Uygulama Cozumleri</title>" +
		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />" +
		"<meta http-equiv=\"content-language\" content=\"TR\" />" + 
		"</head>" +
		"<body>" +
		"<center>" +
		"<p><h1>App is opening ...<br/><br/>Please wait ...</h1></p><br/>" + 
		"<p><h2>Bu pencereyi kapatmak için lütfen BACK/GERİ tuşuna veya aşağıdaki butona tıklayınız.</h2></p>" +
		"<p><input type=\"button\" onClick=\"javascript&colon;top.window.close()\" value=\"Pencereyi Kapat\"></p>" +
		"<br/>" +
		"<p><h3>Not : Butonun çalışması için Tarayıcı ayarlarınızda 'Javascript'i Destekle' seçeneğinin AKTİF yapılması gerekmektedir.</h3></p>" +
		"</center>" +
		"</body>" + 
		"</html>";
		
		byte[] staticPageData = staticPageDataString.getBytes("UTF-8");
		
		_requestHeaders = new HttpHeaders();
		_responseHeaders = new HttpHeaders();
		
		_responseHeaders.setProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, "text/html");
		
		_resultData = staticPageData;
		
		timerTrigger = new Timer();
		timerTrigger.schedule(new runApplicationTrigger(), 3000);
		
		return this;
	}
	
	private class runApplicationTrigger extends TimerTask
	{
		public void run()
		{
			runApplication("mythirdpartyapp", _url);
		}
	}

	public void runApplication(final String moduleName, final String documentUniversalID)
	{
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			
			public void run() {
				
				timerTrigger.cancel();
				
				int moduleHandle = 0;
				
				// Find Application using moduleName
				moduleHandle = CodeModuleManager.getModuleHandle(moduleName);
				
				// Check application is exist
				if (moduleHandle == 0)
				{
					Status.show("App not exist in your device !", 2000);
					return;
				}
				
				try {
					
					String[] newargs = { documentUniversalID };
					
					ApplicationDescriptor adCurrent = CodeModuleManager.getApplicationDescriptors(moduleHandle)[0];
					
					ApplicationDescriptor adTargetApp = new ApplicationDescriptor(adCurrent, newargs);
					
					int processId = ApplicationManager.getApplicationManager().runApplication(adTargetApp, true);
					
					ApplicationManager.getApplicationManager().requestForeground(processId);
					
				} catch (ApplicationManagerException e){
					Status.show ("(runApplication - ApplicationManagerException) - " + e.getClass().getName() + ": " + e.getMessage());
				} catch (Exception e) {
					Status.show ("(runApplication - Exception) - " + e.getClass().getName() + ": " + e.getMessage());
				}
			}
		});
	}
	
	/**
	 * @see javax.microedition.io.HttpConnection#getURL()
	 */
	public String getURL()
	{
		return "http://" + _url;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getProtocol()
	 */
	public String getProtocol()
	{
		return "http";
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getHost()
	 */
	public String getHost()
	{
		return "mytesthost.com.tr";
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getFile()
	 */
	public String getFile()
	{
		return "";
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getRef()
	 */
	public String getRef()
	{
		return "";
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getQuery()
	 */
	public String getQuery()
	{
		return "";
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getPort()
	 */
	public int getPort()
	{
		return 80;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getRequestMethod()
	 */
	public String getRequestMethod()
	{
		return (_requestMethod == null) ? "GET" : _requestMethod;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#setRequestMethod(String)
	 */
	public void setRequestMethod(String method) throws IOException
	{
		_requestMethod = method;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getRequestProperty(String)
	 */
	public String getRequestProperty(String key)
	{
		return _requestHeaders.getPropertyValue(key);
	}

	/**
	 * @see javax.microedition.io.HttpConnection#setRequestProperty(String, String)
	 */
	public void setRequestProperty(String key, String value) throws IOException
	{
		_requestHeaders.setProperty(key, value);
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getResponseCode()
	 */
	public int getResponseCode() throws IOException
	{
		return 200;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getResponseMessage()
	 */
	public String getResponseMessage() throws IOException
	{
		return "OK";
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getExpiration()
	 */
	public long getExpiration() throws IOException
	{
		String value = getHeaderField( HttpProtocolConstants.HEADER_EXPIRES );
		if (value != null) 
		{
			try 
			{
				return HttpDateParser.parse( value );
			} 
			catch (Exception e) 
			{
			}
		}

		return 0;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getDate()
	 */
	public long getDate() throws IOException
	{
		String value = getHeaderField( HttpProtocolConstants.HEADER_DATE );
		if ( value != null) 
		{
			try 
			{
				return HttpDateParser.parse( value );
			} 
			catch (Exception e) 
			{
			}
		}

		return 0;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getLastModified()
	 */
	public long getLastModified() throws IOException
	{
		// Return current time.
		return System.currentTimeMillis();
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getHeaderField(String)
	 */
	public String getHeaderField(String name) throws IOException
	{
		return _responseHeaders.getPropertyValue(name);
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getHeaderFieldInt(String, int)
	 */
	public int getHeaderFieldInt(String name, int def) throws IOException
	{
		String value = _responseHeaders.getPropertyValue(name);
		try 
		{
			if (value != null) 
			{
				return Integer.parseInt( value );
			}
		} 
		catch( NumberFormatException e ) 
		{
		}

		return def;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getHeaderFieldDate(String, long)
	 */
	public long getHeaderFieldDate(String name, long def) throws IOException
	{
		String value = _responseHeaders.getPropertyValue(name);

		try 
		{
			if (value != null) 
			{
				return HttpDateParser.parse( value );
			}
		} 
		catch( Exception e ) 
		{
		}

		return def;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getHeaderField(int)
	 */
	public String getHeaderField(int n) throws IOException
	{
		return _responseHeaders.getPropertyValue(n);
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getHeaderFieldKey(int)
	 */
	public String getHeaderFieldKey(int n) throws IOException
	{
		return _responseHeaders.getPropertyKey(n);
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getType()
	 */
	public String getType()
	{
		try 
		{
			return getHeaderField( HttpProtocolConstants.HEADER_CONTENT_TYPE );
		} 
		catch( IOException e ) 
		{
			return null;
		}
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getEncoding()
	 */
	public String getEncoding()
	{
		try 
		{
			return getHeaderField( HttpProtocolConstants.HEADER_CONTENT_ENCODING );
		} 
		catch( IOException e ) 
		{
			return null;
		}
	}

	/**
	 * @see javax.microedition.io.HttpConnection#getLength()
	 */
	public long getLength()
	{
		try 
		{
			return getHeaderFieldInt(HttpProtocolConstants.HEADER_CONTENT_LENGTH, -1);
		} 
		catch (IOException e)
		{
		}

		return -1;
	}

	/**
	 * @see javax.microedition.io.HttpConnection#openInputStream()
	 */
	public InputStream openInputStream() throws IOException
	{
		return new ByteArrayInputStream(_resultData);
	}

	/**
	 * @see javax.microedition.io.HttpConnection#openDataInputStream()
	 */
	public DataInputStream openDataInputStream() throws IOException
	{
		return new DataInputStream(openInputStream());
	}

	/**
	 * @see javax.microedition.io.HttpConnection#openOutputStream()
	 */
	public OutputStream openOutputStream() throws IOException
	{
		return new ByteArrayOutputStream();
	}

	/**
	 * @see javax.microedition.io.HttpConnection#openDataOutputStream()
	 */
	public DataOutputStream openDataOutputStream() throws IOException
	{
		return new DataOutputStream(openOutputStream());
	}

	/**
	 * @see javax.microedition.io.HttpConnection#close()
	 */
	public void close() throws IOException
	{
		//
	}
}

 

P.S. I found only 1 un-answered post about this problem and It was very old. So I want to open new one to find solution quickly Smiley Happy Sorry for duplicate. Old Post

 

Regards
Ferhat
www.bestcoder.net

Regular Contributor
Posts: 51
Registered: ‎01-13-2011
My Device: 9800
My Carrier: vodaphone

Re: HttpFilterRegistry (Http Filter) is Invoked Only Once

hey write this code in activate method of UiApplication

HttpFilterRegistry.registerFilter("subhost.mytesthost.com.tr", "com.rim.samples.device.httpfilterdemo.precanned", false);



Vishal Lakhmapurkar
New Contributor
Posts: 6
Registered: ‎08-22-2012
My Device: Blackberry Torch OS 6 and 7
My Carrier: ATT

Re: HttpFilterRegistry (Http Filter) is Invoked Only Once

Hi,

 

      I need help in invoking my webworks app from the link in email.After looking your post you already implemented that.Can you please let me know how you integrated this code into your webworks app.

       Appreciate your help on this.

New Contributor
Posts: 6
Registered: ‎08-22-2012
My Device: Blackberry Torch OS 6 and 7
My Carrier: ATT

Re: HttpFilterRegistry (Http Filter) is Invoked Only Once

Hi,

 

      I need help in invoking my webworks app from the link in email.After looking your post you already implemented that.Can you please let me know how you integrated this code into your webworks app.

       Appreciate your help on this.