Sunday, June 12, 2016

Thursday, May 19, 2016

Invoking Jenkins Job using Jersey Client in Java with Username / Password authentication

General

The following post is document how to invoke a job using Jersery client in Java on Jenkins

Assumptions

  • You are familiar with Jenkins and have permissions to create a new job
  • I've used Jenkins version is 1.651.1 ( I'm guessing it will work on previous versions but I did not check)
  • You are familiar with Maven
  • You have IntelliJ ( I used Community Edition 2016.1)

Setup

Jenkins

Creating the job

  • Connect your Jenknis
  • Click on New Item
  • In Item name type 'hello_world'
  • Select 'Freestyle project'
  • Press ok

Configuring the job

  • Click on the job
  • Press on the configure link from the left
  • Check the 'This build is parameterized'
  • Click on 'Add Parameter'
  • Choose String Parameter
  • In the name type 'firstname' (Leave the Default value and description empty)
  • Scroll down to the Build section
  • Click on  'Add build step' and choose Execute shell
  • type 'echo ${firstname}'

Test the job

  • Click on the 'Build with Parameters'
  • Fill in a name click on the build number (should be #1) in case this job is new
  • Click on Console Output
  • You should see something like this
+ echo checking
checking

Java

Creating the project

  • Open IntelliJ
  • Click on 'Create New Project'
  • Choose maven and click next
  • GroupId: com.tzach.jenkins
  • ArtifactId: jerseyclient
  • Project name: jersey_client_jenkins
  • Open the pom.xml file
  • Add the following
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.22.2</version>
        </dependency>
    </dependencies>

  • Click on Enable auto import
  • Click on Maven from the right and choose compile
  • On the bottom you should see 'Process finished with exit code 0'

Now let's write some code

  • Create a new class named JenkinsJerseyClient
  • Create the following method: Response invoke(String username, String password, String firstname)
  • Paste the following
String target = "http://myjenkins:8080/job/hello_world/buildWithParameters";

HttpAuthenticationFeature httpAuthenticationFeature = HttpAuthenticationFeature.basicBuilder()
                .credentials(username, password)
                .build();

ClientConfig clientConfig = new ClientConfig();
clientConfig.register(httpAuthenticationFeature);

Client client = ClientBuilder.newClient(clientConfig);

WebTarget webTarget = client.target(target);
// Parameters needed for the job
Form form = new Form();
form.param("firstname", firstname);

// Sending invoke as POST
return webTarget.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));

  • Replace the URL of jenkins to your jenkins
  • Create a main method, and paste the following:
 System.out.println(new JenkinsJerseyClient().invoke("tzachs","tzachs_password","tzach").getStatus());

  • Replace 'tzachs' with your username
  • Replace 'tzachs_password' with your password
  • Replace 'tzach' with the parameters you need
  • Full code looks like this:

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * Created by tzachs on 5/19/2016.
 */
public class JenkinsJerseyClient {

    public Response invoke(String username, String password, String firstname){
        String target = "http://myjenkins:8080/job/hello_world/buildWithParameters";

        HttpAuthenticationFeature httpAuthenticationFeature = HttpAuthenticationFeature.basicBuilder()
                .credentials(username, password)
                .build();

        ClientConfig clientConfig = new ClientConfig();
        clientConfig.register(httpAuthenticationFeature);

        Client client = ClientBuilder.newClient(clientConfig);

        WebTarget webTarget = client.target(target);
        Form form = new Form();
        form.param("firstname", firstname);

        return webTarget.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
    }

    public static void main(String[] args) {
        System.out.println(new JenkinsJerseyClient().invoke("tzachs","tzachs_password","tzach").getStatus());
    }
}

Run

  • Run the main, you should see in the console log 201. If you see it, it's working. Go to jeknins see you have a new build number

Thursday, April 28, 2016

Connecting IntelliJ and JIRA with SSL (java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty)

Connecting IntelliJ and JIRA with SSL (java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty)

My work place change the JIRA connection from HTTP to HTTPS meaning now JIRA was working with SSL. 
Everything was working fine with Chrome but when trying to work with IntelliJ and JIRA using the Atlassian plugin (https://confluence.atlassian.com/display/IDEPLUGIN/Installing+the+IntelliJ+Connector) I've encountered the following error: 


Clicking on Show Details was not that helpful and displayed the following stack trace:

com.atlassian.theplugin.commons.remoteapi.RemoteApiException: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
at com.atlassian.connector.commons.jira.soap.JIRASoapSessionImpl.login(JIRASoapSessionImpl.java:193)
at com.atlassian.connector.commons.jira.JIRASoapAndXmlServerFacade2Impl.testServerConnection(JIRASoapAndXmlServerFacade2Impl.java:154)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.atlassian.connector.commons.jira.JIRAServerFacade2Impl$1.invoke(JIRAServerFacade2Impl.java:93)
at com.sun.proxy.$Proxy41.testServerConnection(Unknown Source)
at com.atlassian.connector.commons.jira.JIRAServerFacade2Impl.testServerConnection(JIRAServerFacade2Impl.java:268)
at com.atlassian.theplugin.commons.jira.IntelliJJiraServerFacade.testServerConnection(IntelliJJiraServerFacade.java:473)
at com.atlassian.theplugin.idea.config.serverconfig.ProductConnector.connect(ProductConnector.java:35)
at com.atlassian.theplugin.ConnectionWrapper.run(ConnectionWrapper.java:63)

The problem

Intellij now needed the Certificate JIRA was using in order to establish a connection

How to solve?

Thanks to the IT guy at my work, I understood I needed to fetch the certificate and tell IntelliJ to use the key store in which I loaded the certificate to.

Here is how to do it.
  • You have JRE / JDK installed
  • Make sure you have openssl.exe (I'm assuming you are on windows). If you don't have you can get it by installing openvpn (get it from here https://openvpn.net/)
  • Open command line
  • Execute: openssl s_client -connect <JIRA_DNS_NAME>:443 where <JIRA_DNS_NAME> should be replaced with the your JIRA dns name / IP (for example openssl s_client -connect jira:443)
  • Copy the output the last command generated from ---BEGIN CERTIFICATE--- to ---END CERTIFICATE-- including those lines to a file named public.cert
  • Execute: keytool -import-alias <NAME_OF_ALIAS> -file public.cert. For example, keytool -import -alias jira -file public.cert
  • It will ask for a password. The default password is 'changeit'
  • Answer yes to 'Trust this certificate'
  • You should receive 'Certificate was added to keystore'
  • Verify the certificate was really added using the following command: keytool -list | findstr <NAME_OF_ALIAS>, for exampe keytool -list | findstr jira
  • You should see something like this: jira, Apr 28, 2016, trustedCertEntry,
  • Go to the installation of IntelliJ. Default installation in windows is at 'C:\Program Files (x86)\JetBrains\<INTELLIJ_PRODUCT>\bin'
  • If you have multiple installation, notice which one you choose, in my case it was 'C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1.1\bin'
  • Edit the file idea.exe.vmoptions (or idea64.exe.vmoptions, depends which one you are running)
  • Add the following: -Djavax.net.ssl.trustStore=c:\Users\<YOUR USERNAME>\.keystore, for example, in my case it was '-Djavax.net.ssl.trustStore=c:\Users\tzachs\.keystore'
  • Restart IntelliJ

Troubleshoot

In case something does not work, check the idea.log. 

For more reading


Sunday, April 3, 2016

Java script not working in Jenkins due new Content security policy, chrome console log 'displays allow-scripts' permission is not set.

Problem

We run tests using our Jenkins CI and our report contains java script. From version 1.625 LTS we saw that our report is corrupted, meaning most of it is gone.

Displaying the console (F12 in chrome) revealed the problem:
Blocked script execution in 'http://10.10.10.10:8080/job/test/ws/run/report/index.html' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.

Temporary solution:

  • Go to Jenkins main page
  • Click on Manage Jenkins
  • Click on Script Console
  • Paste the following and press run: System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
Go again to the page the was "corrupted" and refresh it

NOTICE: This fix does not survive restart of Jenkins

Fixed Solution:

Ubuntu

  • SSH the jenkins machine
  • Edit the jenkins default file located at /etc/default/jenkins
  • Search for JAVA_ARGS parameter
  • Add the following: -Dhudson.model.DirectoryBrowserSupport.CSP=\"\""
  • Restart jenkins

CentOS


  • SSH the jenkins machine
  • Edit the jenkins default file located at /etc/sysconfig/jenkins
  • Search for JENKINS_JAVA_OPTIONS parameter
  • Add the following: -Dhudson.model.DirectoryBrowserSupport.CSP=\"\""
  • Restart jenkins

See this for more information



Checkout with SVNKIT on UNIX - Solving SVN authentication error E170001

Prefix

I'm writing this post since I've seen many encountered this error without an answer including myself and SVNKIT documentation is not at best, for example:


Problem

The following code will run okay in case you in the windows and working in the domain same as the SVN but will not work on UNIX platforms


SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
SvnCheckout svnCheckout = svnOperationFactory.createCheckout();
svnCheckout.setSingleTarget(SvnTarget.fromFile(checkoutFolder));
svnCheckout.setSource(SvnTarget.fromURL(url));
svnCheckout.run();

Solution:

Add the following line after creating the SvnOperationFactory


svnOperationFactory.setAuthenticationManager(SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray()));

Complete code should look like this:

SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
svnOperationFactory.setAuthenticationManager(SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray()));
SvnCheckout svnCheckout = svnOperationFactory.createCheckout();
svnCheckout.setSingleTarget(SvnTarget.fromFile(checkoutFolder));
svnCheckout.setSource(SvnTarget.fromURL(url));
svnCheckout.run();

Saturday, September 5, 2015

Connecting to Oracle Database using SQL Developer and Wallet authentication method

The story behind this post

A friend encountered a trouble opening a connection using SQL Developer to oracle database using Wallet authentication method.

The ORA error code we got was ORA-12578 TNS: wallet open failed.
After an hour of trying to troubleshoot we were able to make it work.

This post documents how to use the Oracle Sql developer while using the wallet authentication

Assumptions

  • Your Wallet already setup at the database, if you need any help see http://www.dba-resources.com/oracle/a-guide-to-oracle-wallet/
    • You should have the files cwallet.sso and ewallet.p12 files at the end of the process
  • Your Wallet storage is setup on your Client (where the SQL developer is installed)
  • Your TNS is setup with the connection to the database, example set is:
TEST_WALLET_ALIAS = 
    ( DESCRIPTION  =  
       ( ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.17.2)(PORT = 5138))
       ( CONNECT_DATA = 
          (SERVICE_DATA = demo_srv)
       )
    )

  • Your sqlnet.ora have the needed configuration. Our at the client side sqlnet.ora looked like this:
WALLET_LOCATION =
    ( SOURCE =
      ( METHOD = FILE)
      ( METHOD_DATA = 
        ( DIRECTORY = c:\app\product\11.2.0\client_2\network\admin)
      )
    )

Preparing

Verifying Wallet connection is working locally

Before trying to work remotely using the SQL developer, verify the wallet is working by doing the following (NOTICE: This requires access to the database machine itself. Not always you have that access rights. If you do not, try):
  1. Connect to the machine (using SSH or Remote Desktop)
  2. Connect locally using the wallet authentication method using the following syntax:
    1. sqlplus /@<your wallet name> (For example  sqlplus /@TEST_WALLET_ALIAS
  3. If you are able to connect on the database itself continue.

Verifying Wallet connection works remotely using sqlplus

If you have installed the oracle client correctly you probably have the sqlplus client (Oracle's proprietary client).

We'll use it to verify the Wallet is working remotly
  1. Open terminal or command line
  2. Go to your oracle client installation directory. Our's was at c:\app\product\11.2.0\client_2)
  3. Go to the network admin directory.
  4. Copy the sqlnet.ora, tnsnames.ora and wallet files from the database. The tnsnames.ora should have the alias, in our case it was TEST_WALLET_ALIAS
  5. Connect using the syntax of the wallet authentication
    1. sqlplus /@<your wallet name> (For example  sqlplus /@TEST_WALLET_ALIAS

Connecting using SQL Developer

If you've reached this point, I'm assuming sqlplus worked for you at least remotely.
So, in order to make the SQL Developer use the wallet authentication we'll do the following

  1. Verify that you have environment variable named TNS_ADMIN in the system environment variables pointing to the directory where the sqlnet.ora directory resides,
    1. To configure environment variables on windows, go to Control Panel --> System Properties --> Environment Variables
  2. In the sqlnet.ora you need to direct to wallet directory.
  3. Create a wallet using the wallet manager
  4. Copy the wallet files from the Oracle database to the wallet directory
  5. Create a new advanced connection using the following format
    1. Connection Name: TestWaller (the name does not really matter)
    2. Connection Type: Advanced
    3. Custom JDBC URL: jdbc:oracle:oci /@<your wallet alias> (In our case it was jdbc:oracle:oci /@TEST_WALLET_ALIAS)

Further reading

http://www.dba-resources.com/oracle/a-guide-to-oracle-wallet/

Tuesday, August 18, 2015

How to create a Hello World with IntelliJ and Aspect J

General

Since the whole AOP programming method is new to me AND it took me a while to understand how to work with AspectJ and IntelliJ I've decided to create this brief tutorial on how to setup simple Hello World project using AspectJ and IntellJ.

The Ingredients 

  1. IntelliJ Ultimate (I used version 14, you can get it from here: https://www.jetbrains.com/idea/download/)
    1. NOTICE: You must have the ultimate edition. I was not able to use AspectJ with community edition!
    2. I hope you know how to install IntellJ, basically it's next next next.
  2. AspectJ installation (I've used version 1.8.6, you can get the installation JAR here http://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.8.6.jar)
    1. You need to install the jar by double clicking on it in Windows or just run the command from command line 'java -jar aspectj-1.8.6.jar'
    2. NOTICE: Remember where the aspect installtion path since we'll need it later. In windows the default is c:\ (My installation was to c:\aspectj1.8)

So, let's start...

Configuration

  1. Start the IntelliJ
  2. Click on Create new project
  3. Choose Java project
  4. Don't use template, just click next
  5. Name the project AspectJDemoHelloWorld
  6. Click Finish
  7. Open the settings (in windows Ctrl+Alt+S)
  8. Type compiler in the top search field, Chooe the Java Compiler. You should see something like this
  9. Click on Use compiler and choose Ajc (That is the AspetJ compiler)
  10. Click on the 'Path to Ajc compiler' browse button (near the Test button) and choose the 'aspectjtools.jar' at the lib directory of the AspectJ installation (In my case, C:\aspectj1.8\lib\aspectjtools.jar)
  11. Click on the Test button. You should see something like this 
  12. In the search field, type plugins. You should something like this
  13. Click on the search field inside the plugins window and type AspectJ. Verify both plugins are enabled
  14. Press ok
  15. Open the project preferences (Ctrl + Alt + Shift + S in windows)
  16. Click on Modules in the left navigation pane and then click Dependencies.
  17. Click on the plus sign and choose JARs or directories.
  18. Go to the installation folder of the AspectJ and choose  the file 'aspectjrt.jar'
  19. Press OK.
  20. That's for the configuration part

Coding


  1. Go to your src directory
  2. Create a new JAVA class named Hello, Paste in the following
  3. public class Hello {
    
        public static void main(String[] args) {
            sayHello();
        }
    
        public static void sayHello() {
            System.out.print("Hello ");
        }
    }
    
    







  4. Create a new Aspect. Right click and choose Aspect (This ability was enabled due to adding of aspectjrt.jar)
  5. Name the Aspect World and click ok
  6. Paste the following in the aspect

  7. public aspect World {

        pointcut greeting() : execution (* Hello.sayHello(..));
    
        after() returning() : greeting() {
            System.out.println("World");
        }
    }
  8. Click on Build --> Rebuild project
  9. Right click on Hello.java and choose Run
  10. You should see 'Hello World' in the System console
If you only see Hello, that means the Aspect code was not compiled meaning, you should check the Ajc compiler is the compiler chosen (see step 7 in the configuration part)

Thursday, August 13, 2015

Creating javadocs without the package name before objects (with and without ant)

General


This is a simple one but from some reason it took a while to find the solution.

When creatin javadocs, by default each object comes with it package name.
If you don't want to see the package name before each object, use the noqualifier flag (thanks to http://stackoverflow.com/questions/10766238/javadoc-parameter-without-package-name for the solution)

As ant target


Example Javadoc ant taget


<target name="javadocs" >
    <mkdir dir="${dir.javadocs}"/>

    <javadoc packagenames="com.tzachsolomon.example.*"             sourcepath="-module1/src/main/java:module2/src/main/java"             destdir="${dir.javadocs}"             classpath="module1/src/main/java:module2/src/main/java"
             author="true"             version="true"             use="true"             access="public"             windowtitle="${app.name} API"             encoding="UTF-8"             verbose="false"             noqualifier="all"             doctitle="&lt;h1&gt;${app.name}&lt;/h1&gt;"/>
</target>

Thursday, May 21, 2015

JAVA - Separate log files for threads using log4j2

General

I've created this page to document how to create separate log file (in separate directory) using log4j2.
It contains a small example (1 Main thread and 2 test threads), how to and explanation how it works.

I hope you'll find it useful

This tutorial is based on the post at http://stackoverflow.com/questions/25114526/log4j2-how-to-write-logs-to-separate-files-for-each-user!

Assumptions

  1. You are familiar with JAVA
  2. You are familiar with log4j2 configuration
  3. You know how to link JARs (although I explain how to do it in IntelliJ)
Still, if you need help since something is unclear don't hesitate to contact me :)

Prerequisites 

  1. Download log4j2 from https://logging.apache.org/log4j/2.0/download.html (Current version 2.3) 
  2. Download  slf4j from http://www.slf4j.org/download.html (Current version 1.7.12)

How to:

you download the project https://github.com/tzachs/test-log4j2-threads or do the following yourself
  1. Start IntelliJ
  2. Create a new project named test-log4j2-threads
  3. Create a directory named lib
  4. Move the files apache-log4j-2.3-bin.tar.gz and slf4j-1.7.12.tar.gz to the lib directory
  5. Extract the files in the directory
  6. Create 3 classes
    1. Main.java
    2. TestThread.java
    3. MyLogger.java
  7. Create a file named log4j2.xml
  8. Copy the following to Main.java
  9. import org.apache.logging.log4j.ThreadContext;
    
    /**
     * Created by tzach on 5/16/15.
     */
    public class Main {
    
        public static void main(String[] args) {
            ThreadContext.put("logFilename","main");
            MyLogger.init();
    
    
            int i;
            MyLogger.getLogger().info("Started");
            TestThread testThread;
            testThread = new TestThread("test1");
            testThread.start();
            testThread = new TestThread("test2");
            testThread.start();
    
            for ( i = 0; i < 4; i++){
                try {
    
                    MyLogger.getLogger().debug("GUI log");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            MyLogger.getLogger().info("Finished");
        }
    }
    
  10. Copy the following code to TestThread.java
  11. import org.apache.logging.log4j.ThreadContext;
    
    /**
     * Created by tzach on 5/16/15.
     */
    public class TestThread extends Thread implements Runnable {
    
        private final String threadName;
    
        public TestThread(String threadName){
            this.threadName = threadName;
        }
    
        @Override
        public void run() {
            Thread.currentThread().setName(threadName);
            ThreadContext.put("logFilename",   Thread.currentThread().getName());
    
            MyLogger.getLogger().debug("Starting new loop");
            int i = 0;
            long threadId = Thread.currentThread().getId();
            for ( int z = 0; z < 3; z++){
                MyLogger.getLogger().warn("Thread number: " + threadId + " message number " + ++i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    MyLogger.getLogger().error(e.toString(),e);
                }
            }
        }
    }
    
  12. Copy the following code to TestThread.java
  13. import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    /**
     * Created by tzach on 5/16/15.
     */
    public class MyLogger {
    
        private static Logger logger;
    
        public static void init(){
            logger = LogManager.getLogger("test-log4j2-threads");
        }
    
        public static Logger getLogger(){
            return logger;
        }
    }
    
  14. Copy the following to log4j2.xml
  15. <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            </Console>
    
            <Routing name="RoutingAppender">
                <Routes pattern="$${ctx:logFilename}">
                    <Route>
                        <RollingFile name="Rolling-${ctx:logFilename}" fileName="logs/${ctx:logFilename}/test.log"
                                     filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
                            <PatternLayout pattern="%d{ABSOLUTE} %level{length=5} [%thread] %logger{1} - %msg%n"/>
                            <SizeBasedTriggeringPolicy size="512" />
                        </RollingFile>
                    </Route>
    
                    <!-- By having this set to ${ctx:logFileName} it will match when filename
                         is not set in the context -->
                    <Route ref="Console" key="${ctx:logFileName}"/>
                </Routes>
            </Routing>
        </Appenders>
        <Loggers>
            <Root level="trace">
                <AppenderRef ref="RoutingAppender"/>
            </Root>
        </Loggers>
    </Configuration>
    
  16. Link the libraries. In Intellij you'll do it by doing the following:
    1. Open the Project Structure (Setting). In Ubunutu you can press Ctrl+Alt+Shift+S
    2. Go to Modules
    3. Press on the Dependencies tab
    4. Click on the plus sign and add the JARs. You should see something like this in the end of the process.
  17. Click on build --> Make (Or Ctrl+F9)
  18. Press on the run (or Shift+F10)
  19. You should see a directory named logs  created with the following structure:
    1. logs
      1. main
        1. test.log
      2. test1
        1. test.log
      3. test2
        1. test.log
  20. Notice that each test.log has messages only from it's own thread
  21. That's it :)

Explanation

So what is actually going on?

As you can see, I've used Routing Appender which is named RoutingAppender in my example.
The pattern to of route is depended on the variable in each Thread context named logFilename.

Notice that I do not handle any thing related to the creating of the files or the directories, it is all done by the log4j2.

All you need to do, is to put a name of the directory you want be created for each Thread, for example, Line number 9 in class Main.java:
ThreadContext.put("logFilename","main")

By typing "main", I've decided that all the logs for the main thread will go to the directory logs/main

For more info see https://logging.apache.org/log4j/2.0/faq.html#separate_log_files

Saturday, February 9, 2013

How to alert user on change in database using AJAX, MYSQL, PHP?

Hi,

I have a simple web page where multiple users are editing the same rows at the same time.
I needed a simple way to alert the users, if the page they were working on is no longer valid.
Meaning, it was updated in the database and they should refresh the page (MY NEXT TASK IS TO AUTO UPDATE).

This example document how to alert the user that the database has been changed and he/she should refresh the page.

Assumptions:
1. You have a working MySQL database with the database named checkupdate
2. In the checkupdate database there is a table named tbl1 with one integer column named _id

Let's start...

Create the following files:
index.html

<!DOCTYPE html>
<html>
<head>
 <script type="text/javascript" src="checkupdate.js"> </script>
</head>
 <body onload="process()">
                Last update:
  <div id="underInput" />
 </body>
</html>

checkupdate.js


var xmlHttp = createXmlHttpRequestObject();
var nIntervalId;
var underInput;

function process () {
 nIntervalId = setInterval(checkUpdate1, 10000);
 checkUpdate1 ();
}

function checkUpdate1 () {

 xmlHttp.open("GET", "checkupdate.php?t="+Math.random(), true);
 xmlHttp.onreadystatechange = handleServerResponse;
 xmlHttp.send();

}

function handleServerResponse () {

 //console.log(xmlHttp.readyState);
 underInput = encodeURIComponent(document.getElementById("underInput").innerHTML);
 if ( xmlHttp.readyState==4 )
  if ( xmlHttp.status==200) {
  xmlResponse = xmlHttp.responseXML;
  xmlDocumentElement = xmlResponse.documentElement;
  message = xmlDocumentElement.firstChild.textContent;
  message1 = parseInt(message);
  underInput = parseInt(underInput);
  if ( message1 > underInput ) {
   alert ("Change was made in db, please refresh the page");
  }
  document.getElementById("underInput").innerHTML = message;
 }
}

function createXmlHttpRequestObject() {

 var xmlHttp;

 if (window.ActiveXObject){
  try{
   xmlHttp = new ActiveXObject("Microsofot.XMLHTTP");
  } catch (e) {
   xmlHttp = false;
  }
 }else{
  try{
   xmlHttp = new XMLHttpRequest();
  } catch (e) {
   xmlHttp = false;
  }
 }

 if (!xmlHttp) {
  alert("Could not create XML Object");
 } else {
  return xmlHttp;
 }
}

checkupdate.php


<?php

$conn = mysql_connect('localhost','root','helloworld');

mysql_select_db('checkupdate', $conn);

$query = "SELECT MAX(_id) FROM tbl1";

$result = mysql_query($query,$conn);

$row = mysql_fetch_array($result);

mysql_close($conn);
 header('Content-Type: text/xml');

 echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
 echo '<response>';
 echo "{$row['MAX(_id)']}";
 echo '</response>';

?>



Thursday, February 7, 2013

AJAX, First Example (Based on the new Boston website)

Hi,

As I mentioned, this blog is mostly for me to remember the code I'm learning so I won't be doing much explaining of the code BUT however, I will place links in case I took the examples from some source.

Sources

1. Example was basically taken from http://www.youtube.com/watch?v=tp3Gw-oWs2k, I just change'd it a bit since it wasn't really working.
2. http://www.w3schools.com/ajax/

Assumptions:
1. You have a working web server supporting php
2. You are familiar with PHP
3. You are familiar with JavaScript

So lets start!
1. Create a file name index.html and copy the code below to the file

<!DOCTYPE html>
<html>
<head>
 <script type="text/javascript" src="foodstore.js"> </script>
</head>
 <body>
  <input type="text" id="userInput" onkeyup="process()" />
  <div id="underInput" />
 </body>
</html>

2. Create a file name foodstore.js and copy the code below to the file

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {

 var xmlHttp;

 if (window.ActiveXObject){
  try{
   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e) {
   xmlHttp = false;
  }
 }else{
  try{
   xmlHttp = new XMLHttpRequest();
  } catch (e) {
   xmlHttp = false;
  }
 }

 if (!xmlHttp) {
  alert("Could not create XML Object");
 } else {
  return xmlHttp;
 }
}

function process() {


  food = encodeURIComponent(document.getElementById("userInput").value);
  //alert(food);
  xmlHttp.open("GET", "foodstore.php?food="+food, true);
  xmlHttp.onreadystatechange = handleServerResponse;
  xmlHttp.send();

}

function handleServerResponse () {

 if ( xmlHttp.readyState==4 )
  if ( xmlHttp.status==200 || xmlHttp.status == 206) {
  xmlResponse = xmlHttp.responseXML;
  xmlDocumentElement = xmlResponse.documentElement;
  message = xmlDocumentElement.firstChild.textContent;
  document.getElementById("underInput").innerHTML = '<span style="color:blue">' + message + '</span>';
 }
}

3. Create a file name foodstore.php and copy the code below to the file

<?php
 header('Content-Type: text/xml');
 echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

 echo '<response>';
  $food=$_GET['food'];
  $foodArray = array('tuna','bacon','beef','loaf','ham');

  if (in_array($food,$foodArray)) {
   echo "We do have ".$food;
  } elseif ($food==''){
   echo "Enter food";
  } else {
   echo "Sorry, we do not have ".$food;
  }

 echo '</response>';
?>

4. Place the 3 files in a directory under your web server (meaning you can browse it)

5. Surf to the index.html file.

6.You should see text box. Type your name in it and you should see something like this below the text box:
Sorry, we do not have Tzach

7. Type in tune and you should see the following:
We do have tuna

Hello World?!

Hi,

My name is Tzach, 30 years old and I know it's kinda corny but I though to just type Hello World as a good example of posts in the blog.

The whole purpose of this blog is to save and remember  code examples or solutions to problems I've encountered during work or coding at home.

I hope that the code and explanations will be coherent enough so that it might use others.

Till next time,
Tzach