Information Technology Security (Developing Enterprise Systems)

profiledanish.dan
ProjectPart_2.zip

productMaint2/build.xml

Builds, tests, and runs the project productMaint2.

productMaint2/nbproject/ant-deploy.xml

productMaint2/nbproject/build-impl.xml

Must set src.dir Must set test.src.dir Must set build.dir Must set build.web.dir Must set build.generated.dir Must set dist.dir Must set build.classes.dir Must set dist.javadoc.dir Must set build.test.classes.dir Must set build.test.results.dir Must set build.classes.excludes Must set dist.war The Java EE server classpath is not correctly set up - server home directory is missing. Either open the project in the IDE and assign the server or setup the server classpath manually. For example like this: ant -Dj2ee.server.home=<app_server_installation_directory> The Java EE server classpath is not correctly set up. Your active server type is ${j2ee.server.type}. Either open the project in the IDE and assign the server or setup the server classpath manually. For example like this: ant -Duser.properties.file=<path_to_property_file> (where you put the property "j2ee.platform.classpath" in a .properties file) or ant -Dj2ee.platform.classpath=<server_classpath> (where no properties file is used) Must set javac.includes No tests executed. The libs.CopyLibs.classpath property is not set up. This property must point to org-netbeans-modules-java-j2seproject-copylibstask.jar file which is part of NetBeans IDE installation and is usually located at <netbeans_installation>/java<version>/ant/extra folder. Either open the project in the IDE and make sure CopyLibs library exists or setup the property manually. For example like this: ant -Dlibs.CopyLibs.classpath=a/path/to/org-netbeans-modules-java-j2seproject-copylibstask.jar Must set JVM to use for profiling in profiler.info.jvm Must set profiler agent JVM arguments in profiler.info.jvmargs.agent Must select some files in the IDE or set javac.includes Must select some files in the IDE or set javac.jsp.includes Must select a file in the IDE or set jsp.includes Browser not found, cannot launch the deployed application. Try to set the BROWSER environment variable. Launching ${browse.url} Must select one file in the IDE or set run.class Must select one file in the IDE or set run.class Must select one file in the IDE or set debug.class Must select one file in the IDE or set debug.class Must set fix.includes This target only works when run from inside the NetBeans IDE. Must select some files in the IDE or set javac.includes Some tests failed; see details above. Must select some files in the IDE or set test.includes Some tests failed; see details above. Must select some files in the IDE or set test.class Must select some method in the IDE or set test.method Some tests failed; see details above. Must select one file in the IDE or set test.class Must select one file in the IDE or set test.class Must select some method in the IDE or set test.method

productMaint2/nbproject/genfiles.properties

build.xml.data.CRC32=a9dcf550 build.xml.script.CRC32=666d2292 [email protected] # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. nbproject/build-impl.xml.data.CRC32=a9dcf550 nbproject/build-impl.xml.script.CRC32=955abc45 nbproject/[email protected]

productMaint2/nbproject/private/private.properties

deploy.ant.properties.file=C:\\Users\\Joel\\AppData\\Roaming\\NetBeans\\8.0\\tomcat80.properties j2ee.server.home=C:/tomcat-8.0 j2ee.server.instance=tomcat80:home=C:\\tomcat-8.0 javac.debug=true javadoc.preview=true selected.browser=default user.properties.file=C:\\Users\\Joel\\AppData\\Roaming\\NetBeans\\8.0\\build.properties

productMaint2/nbproject/private/private.xml

productMaint2/nbproject/project.properties

annotation.processing.enabled=true annotation.processing.enabled.in.editor=true annotation.processing.processors.list= annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output build.classes.dir=${build.web.dir}/WEB-INF/classes build.classes.excludes=**/*.java,**/*.form build.dir=build build.generated.dir=${build.dir}/generated build.generated.sources.dir=${build.dir}/generated-sources build.test.classes.dir=${build.dir}/test/classes build.test.results.dir=${build.dir}/test/results build.web.dir=${build.dir}/web build.web.excludes=${build.classes.excludes} client.urlPart= compile.jsps=false conf.dir=${source.root}/conf debug.classpath=${build.classes.dir}:${javac.classpath} debug.test.classpath=\ ${run.test.classpath} display.browser=true dist.dir=dist dist.ear.war=${dist.dir}/${war.ear.name} dist.javadoc.dir=${dist.dir}/javadoc dist.war=${dist.dir}/${war.name} endorsed.classpath= excludes= includes=** j2ee.compile.on.save=false j2ee.copy.static.files.on.save=false j2ee.deploy.on.save=false j2ee.platform=1.5 j2ee.platform.classpath=${j2ee.server.home}/lib/annotations-api.jar:${j2ee.server.home}/lib/catalina-ant.jar:${j2ee.server.home}/lib/catalina-ha.jar:${j2ee.server.home}/lib/catalina-storeconfig.jar:${j2ee.server.home}/lib/catalina-tribes.jar:${j2ee.server.home}/lib/catalina.jar:${j2ee.server.home}/lib/ecj-P20140317-1600.jar:${j2ee.server.home}/lib/el-api.jar:${j2ee.server.home}/lib/jasper-el.jar:${j2ee.server.home}/lib/jasper.jar:${j2ee.server.home}/lib/jsp-api.jar:${j2ee.server.home}/lib/mysql-connector-java-5.1.23-bin.jar:${j2ee.server.home}/lib/servlet-api.jar:${j2ee.server.home}/lib/tomcat-api.jar:${j2ee.server.home}/lib/tomcat-coyote.jar:${j2ee.server.home}/lib/tomcat-dbcp.jar:${j2ee.server.home}/lib/tomcat-i18n-es.jar:${j2ee.server.home}/lib/tomcat-i18n-fr.jar:${j2ee.server.home}/lib/tomcat-i18n-ja.jar:${j2ee.server.home}/lib/tomcat-jdbc.jar:${j2ee.server.home}/lib/tomcat-jni.jar:${j2ee.server.home}/lib/tomcat-spdy.jar:${j2ee.server.home}/lib/tomcat-util-scan.jar:${j2ee.server.home}/lib/tomcat-util.jar:${j2ee.server.home}/lib/tomcat-websocket.jar:${j2ee.server.home}/lib/websocket-api.jar j2ee.server.type=Tomcat jar.compress=false javac.classpath=\ ${libs.jstl.classpath} # Space-separated list of extra javac options javac.compilerargs= javac.debug=true javac.deprecation=false javac.processorpath=\ ${javac.classpath} javac.source=1.8 javac.target=1.8 javac.test.classpath=\ ${javac.classpath}:\ ${build.classes.dir}:\ ${libs.junit.classpath}:\ ${libs.junit_4.classpath} javac.test.processorpath=${javac.test.classpath} javadoc.additionalparam= javadoc.author=false javadoc.encoding=${source.encoding} javadoc.noindex=false javadoc.nonavbar=false javadoc.notree=false javadoc.preview=true javadoc.private=false javadoc.splitindex=true javadoc.use=true javadoc.version=false javadoc.windowtitle= jspcompilation.classpath=${jspc.classpath}:${javac.classpath} lib.dir=${web.docbase.dir}/WEB-INF/lib persistence.xml.dir=${conf.dir} platform.active=default_platform resource.dir=setup run.test.classpath=\ ${javac.test.classpath}:\ ${build.test.classes.dir} # Space-separated list of JVM arguments used when running a class with a main method # (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value): runmain.jvmargs= source.encoding=UTF-8 source.root=src src.dir=${source.root}/java test.src.dir=test war.content.additional= war.ear.name=productMaint2.war war.name=productMaint2.war web.docbase.dir=web webinf.dir=web/WEB-INF

productMaint2/nbproject/project.xml

org.netbeans.modules.web.project productMaint2 1.6 ${libs.jstl.classpath} WEB-INF/lib

productMaint2/src/conf/MANIFEST.MF

Manifest-Version: 1.0

productMaint2/src/java/music/admin/ProductAdminController.java

productMaint2/src/java/music/admin/ProductAdminController.java

package  music . admin ;

import  java . io . IOException ;
import  java . util . ArrayList ;
import  javax . servlet . ServletException ;
import  javax . servlet . http . HttpServlet ;
import  javax . servlet . http . HttpServletRequest ;
import  javax . servlet . http . HttpServletResponse ;
import  music . business . Product ;
import  music . data . ProductIO ;

public   class   ProductAdminController   extends   HttpServlet   {
    
     /* Comment this method out when using this class with a database
     * instead of a text file.
     */
    @ Override
     public   void  init ()   {
         ProductIO . init ( getServletContext ()
                 . getRealPath ( "/WEB-INF/products.txt" ));
     }

    @ Override
     public   void  doGet ( HttpServletRequest  request ,   HttpServletResponse  response )
             throws   ServletException ,   IOException   {

         // get current action
         String  action  =  request . getParameter ( "action" );
         if   ( action  ==   null )   {
            action  =   "displayProducts" ;    // default action
         }

         // perform action and set URL to appropriate page
         String  url  =   "/index.jsp" ;
         if   ( action . equals ( "displayProducts" ))   {
            url  =  displayProducts ( request ,  response );
         }   else   if   ( action . equals ( "displayProduct" ))   {
            url  =  displayProduct ( request ,  response );
         }   else   if   ( action . equals ( "addProduct" ))   {
            url  =   "/product.jsp" ;
         }   else   if   ( action . equals ( "deleteProduct" ))   {
            url  =  deleteProduct ( request ,  response );
         }
        getServletContext ()
                 . getRequestDispatcher ( url )
                 . forward ( request ,  response );
     }

    @ Override
     public   void  doPost ( HttpServletRequest  request ,   HttpServletResponse  response )
             throws   ServletException ,   IOException   {

         // get current action
         String  action  =  request . getParameter ( "action" );
         if   ( action  ==   null )   {
            action  =   "displayProducts" ;    // default action
         }

         // perform action and set URL to appropriate page
         String  url  =   "/index.jsp" ;
         if   ( action . equals ( "updateProduct" ))   {
            url  =  updateProduct ( request ,  response );
         }   else   if   ( action . equals ( "deleteProduct" ))   {
            url  =  deleteProduct ( request ,  response );
         }
        getServletContext ()
                 . getRequestDispatcher ( url )
                 . forward ( request ,  response );
     }

     private   String  displayProducts ( HttpServletRequest  request ,
             HttpServletResponse  response )   {

         ArrayList < Product >  products  =   ( ArrayList )   ProductIO . selectProducts ();
        request . setAttribute ( "products" ,  products );
         return   "/products.jsp" ;
     }

     private   String  displayProduct ( HttpServletRequest  request ,
             HttpServletResponse  response )   {

         String  productCode  =  request . getParameter ( "productCode" );
         Product  product ;
         if   ( productCode  ==   null   ||  productCode . isEmpty ())   {
            product  =   new   Product ();
         }   else   {
            product  =   ProductIO . selectProduct ( productCode );
         }

        request . setAttribute ( "product" ,  product );
         return   "/product.jsp" ;
     }

     private   String  addProduct ( HttpServletRequest  request ,
             HttpServletResponse  response )   {

         return   "/product.jsp" ;
     }

     private   String  updateProduct ( HttpServletRequest  request ,
             HttpServletResponse  response )   {

         String  productCode  =   ( String )  request . getParameter ( "productCode" );
         String  description  =   ( String )  request . getParameter ( "description" );
         String  priceString  =   ( String )  request . getParameter ( "price" );

         double  price ;
         try   {
            price  =   Double . parseDouble ( priceString );
         }   catch   ( NumberFormatException  e )   {
            price  =   0 ;
         }

         Product  product  =   ( Product )  request . getAttribute ( "product" );
         if   ( product  ==   null )   {
            product  =   new   Product ();
         }
        product . setCode ( productCode );
        product . setDescription ( description );
        product . setPrice ( price );
        request . setAttribute ( "product" ,  product );

         String  message  =   "" ;
         if   ( product . getPrice ()   <=   0 )   {
            message  =   "You must enter a positive number for the price without "
                     +   "any currency symbols." ;
         }
         if   ( product . getDescription (). length ()   ==   0 )   {
            message  =   "You must enter a description for the product." ;
         }
         if   ( product . getCode (). length ()   ==   0 )   {
            message  =   "You must enter a code for the product." ;
         }
        request . setAttribute ( "message" ,  message );

         String  url ;
         if   ( message . isEmpty ())   {
             if   ( ProductIO . exists ( product . getCode ()))   {
                 ProductIO . updateProduct ( product );
             }   else   {
                 ProductIO . insertProduct ( product );
             }
            url  =  displayProducts ( request ,  response );
         }   else   {
            url  =   "/product.jsp" ;
         }
         return  url ;
     }
    
     private   String  deleteProduct ( HttpServletRequest  request ,
             HttpServletResponse  response )   {

         String  productCode  =  request . getParameter ( "productCode" );
         Product  product  =   ProductIO . selectProduct ( productCode );
        request . setAttribute ( "product" ,  product );
        
         String  url ;
         String  yesButton  =  request . getParameter ( "yesButton" );
         if   ( yesButton  !=   null )   {
             ProductIO . deleteProduct ( product );
            url  =  displayProducts ( request ,  response );
         }   else   {
            url  =   "/confirm_product_delete.jsp" ;
         }
         return  url ;
     }     
}

productMaint2/src/java/music/business/Product.java

productMaint2/src/java/music/business/Product.java

package  music . business ;

import  java . text . NumberFormat ;
import  java . io . Serializable ;

public   class   Product   implements   Serializable   {

     private   String  code ;
     private   String  description ;
     private   double  price ;

     public   Product ()   {
        code  =   "" ;
        description  =   "" ;
        price  =   0 ;
     }

     public   void  setCode ( String  code )   {
         this . code  =  code ;
     }

     public   String  getCode ()   {
         return  code ;
     }

     public   void  setDescription ( String  description )   {
         this . description  =  description ;
     }

     public   String  getDescription ()   {
         return  description ;
     }

     public   void  setPrice ( double  price )   {
         this . price  =  price ;
     }

     public   double  getPrice ()   {
         return  price ;
     }

     public   String  getPriceNumberFormat ()   {
         NumberFormat  number  =   NumberFormat . getNumberInstance ();
        number . setMinimumFractionDigits ( 2 );
         if   ( price  ==   0 )   {
             return   "" ;
         }   else   {
             return  number . format ( price );
         }
     }

     public   String  getPriceCurrencyFormat ()   {
         NumberFormat  currency  =   NumberFormat . getCurrencyInstance ();
         return  currency . format ( price );
     }
}

productMaint2/src/java/music/data/ProductIO.java

productMaint2/src/java/music/data/ProductIO.java

package  music . data ;

import  java . io . * ;
import  java . util . * ;

import  music . business . * ;

public   class   ProductIO   {

     private   static   List < Product >  products  =   null ;
     private   static   String  filePath  =   null ;

     // Called once from ProductAdminController based on servlet context
     public   static   void  init ( String  filePath )   {
         ProductIO . filePath  =  filePath ;
     }

     public   static   List < Product >  selectProducts ()   {
        products  =   new   ArrayList < Product > ();
         File  file  =   new   File ( filePath );
         try   {
             BufferedReader  in
                     =   new   BufferedReader (
                             new   FileReader ( file ));

             String  line  =  in . readLine ();
             while   ( line  !=   null )   {
                 StringTokenizer  t  =   new   StringTokenizer ( line ,   "|" );
                 if   ( t . countTokens ()   >=   3 )   {
                     String  code  =  t . nextToken ();
                     String  description  =  t . nextToken ();
                     String  priceAsString  =  t . nextToken ();
                     double  price  =   Double . parseDouble ( priceAsString );

                     Product  p  =   new   Product ();
                    p . setCode ( code );
                    p . setDescription ( description );
                    p . setPrice ( price );

                    products . add ( p );
                 }
                line  =  in . readLine ();
             }
            in . close ();
             return  products ;
         }   catch   ( IOException  e )   {
             System . out . println ( e );
             return   null ;
         }
     }

     public   static   Product  selectProduct ( String  productCode )   {
        products  =  selectProducts ();
         for   ( Product  p  :  products )   {
             if   ( productCode  !=   null
                     &&  productCode . equalsIgnoreCase ( p . getCode ()))   {
                 return  p ;
             }
         }
         return   null ;
     }

     public   static   boolean  exists ( String  productCode )   {
         Product  p  =  selectProduct ( productCode );
         if   ( !=   null )   return   true ;
         else   return   false ;
     }     
    
     private   static   void  saveProducts ( List < Product >  products )   {
         try   {
             File  file  =   new   File ( filePath );
             PrintWriter  out
                     =   new   PrintWriter (
                             new   FileWriter ( file ));

             for   ( Product  p  :  products )   {
                out . println ( p . getCode ()   +   "|"
                         +  p . getDescription ()   +   "|"
                         +  p . getPrice ());
             }

            out . close ();
         }   catch   ( IOException  e )   {
             System . out . println ( e );
         }
     }

     public   static   void  insertProduct ( Product  product )   {
        products  =  selectProducts ();
        products . add ( product );
        saveProducts ( products );
     }

     public   static   void  updateProduct ( Product  product )   {
        products  =  selectProducts ();
         for   ( int  i  =   0 ;  i  <  products . size ();  i ++ )   {
             Product  p  =  products . get ( i );
             if   ( product . getCode ()   !=   null
                     &&  product . getCode (). equalsIgnoreCase ( p . getCode ()))   {
                products . set ( i ,  product );
             }
         }
        saveProducts ( products );
     }

     public   static   void  deleteProduct ( Product  product )   {
        products  =  selectProducts ();
         for   ( int  i  =   0 ;  i  <  products . size ();  i ++ )   {
             Product  p  =  products . get ( i );
             if   ( product  !=   null
                     &&  product . getCode (). equalsIgnoreCase ( p . getCode ()))   {
                products . remove ( i );
             }
         }
        saveProducts ( products );
     }     
}

productMaint2/src/java/music/tags/IfEmptyMarkTag.java

productMaint2/src/java/music/tags/IfEmptyMarkTag.java

package  music . tags ;

import  javax . servlet . jsp . * ;
import  javax . servlet . jsp . tagext . * ;
import  java . io . * ;

public   class   IfEmptyMarkTag   extends   TagSupport   {

     private   String  field ;
     private   String  color  =   "blue" ;

     public   void  setField ( String  field )   {
         this . field  =  field ;
     }

     public   void  setColor ( String  color )   {
         this . color  =  color ;
     }

     public   int  doStartTag ()   throws   JspException   {
         try   {
             JspWriter  out  =  pageContext . getOut ();
             if   ( field  ==   null   ||  field . length ()   ==   0 )   {
                out . print ( "<font color="   +  color  +   "> *</font>" );
             }
         }   catch   ( IOException  ioe )   {
            ioe . printStackTrace ();
         }
         return  SKIP_BODY ;
     }
}

productMaint2/web/confirm_product_delete.jsp

<%@page contentType="text/html" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Product Maintenance</title> <link rel="stylesheet" href="<c:url value='/styles/main.css'/> "> </head> <body> <h1>Are you sure you want to delete this product?</h1> <label>Code:</label> <span>${product.code}</span><br> <label>Description:</label> <span>${product.description}</span><br> <label>Price:</label> <span>${product.priceNumberFormat}</span><br> <form action="" method="post" class="inline"> <input type="hidden" name="action" value="deleteProduct"> <input type="hidden" name="productCode" value="${product.code}"> <input name="yesButton" type="submit" value="Yes" class="confirm_button"> </form> <form action="" method="get" class="inline"> <input type="hidden" name="action" value="displayProducts"> <input type="submit" value="No" class="confirm_button"> </form> </body> </html>

productMaint2/web/index.jsp

<%@page contentType="text/html" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Product Maintenance</title> <link rel="stylesheet" href="<c:url value='/styles/main.css'/> "> </head> <body> <h1>Product Maintenance</h1> <a href="<c:url value='/productMaint?action=displayProducts'/>">View Products</a> </body> </html>

productMaint2/web/META-INF/context.xml

productMaint2/web/product.jsp

<%@page contentType="text/html" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="mma" uri="/WEB-INF/murach.tld" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Product Maintenance</title> <link rel="stylesheet" href="<c:url value='/styles/main.css'/> "> </head> <body> <h1>Product</h1> <p><mma:ifEmptyMark color="blue" field=""/> marks required fields</p> <p><i>${message}</i></p> <form action="<c:url value='/productMaint'/>" method="post" class="inline"> <input type="hidden" name="action" value="updateProduct"> <label class="pad_top">Code:</label> <input type="text" name="productCode" id="codeBox" value="${product.code}"> <mma:ifEmptyMark field="${product.code}"/><br> <label class="pad_top">Description:</label> <input type="text" name="description" value="${product.description}"> <mma:ifEmptyMark field="${product.description}"/><br> <label class="pad_top">Price:</label> <input type="text" name="price" id="priceBox" value="${product.priceNumberFormat}"> <mma:ifEmptyMark field="${product.priceNumberFormat}"/><br> <label class="pad_top">&nbsp;</label> <input type="submit" value="Update Product" class="margin_left"> </form> <form action="<c:url value='/productMaint?action=displayProducts'/>" method="get" class="inline"> <input type="submit" value="View Products"> </form> </body> </html>

productMaint2/web/products.jsp

<%@page contentType="text/html" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Product Maintenance</title> <link rel="stylesheet" href="<c:url value='/styles/main.css'/> "> </head> <body> <h1>Products</h1> <table> <tr> <th>Code</th> <th>Description</th> <th class="right">Price</th> <th></th> <th></th> </tr> <c:forEach var="p" items="${products}"> <tr> <td>${p.code}</td> <td>${p.description}</td> <td class="right">${p.priceCurrencyFormat}</td> <td> <a href="<c:url value='/productMaint?action=displayProduct&productCode=${p.code}'/>">Edit</a> </td> <td> <a href="<c:url value='/productMaint?action=deleteProduct&productCode=${p.code}'/>">Delete</a> </td> </tr> </c:forEach> </table> <form action="<c:url value='/productMaint'/>" method="get" class="pad_top"> <input type="hidden" name="action" value="addProduct"> <input type="submit" value="Add Product"> </form> </body> </html>

productMaint2/web/styles/main.css

/* The styles for the elements */ body { font-family: Arial, Helvetica, sans-serif; font-size: 85%; margin-left: 2em; margin-right: 2em; width: 600px; } h1 { font-size: 140%; color: teal; margin-bottom: .5em; } label { float: left; width: 8em; margin-bottom: 0.5em; font-weight: bold; } input[type="text"], input[type="email"] { /* An attribute selector */ width: 30em; margin-left: 0.5em; margin-bottom: 0.5em; } span { margin-left: 0.5em; margin-bottom: 0.5em; } br { clear: both; } /* The styles for the classes */ .pad_top { padding-top: 0.5em; } .margin_left { margin-left: 0.5em; } /* The styles for the tables */ table { border: 1px solid black; border-collapse: collapse; width: 50em; } th, td { border: 1px solid black; text-align: left; padding: .5em; } .right { text-align: right; } /* The styles for displaying forms and buttons */ .inline { display: inline; } .confirm_button { width: 5em; } #codeBox, #priceBox { width: 6em; }

productMaint2/web/WEB-INF/murach.tld

1.0 murach /WEB-INF/murach.tld A custom tag library developed by Mike Murach and Associates ifEmptyMark music.tags.IfEmptyMarkTag empty color false field true true

productMaint2/web/WEB-INF/products.txt

8601|86 (the band) - True Life Songs and Pictures|14.95 pf01|Paddlefoot - The first CD|12.95 pf02|Paddlefoot - The second CD|14.95 jr01|Joe Rut - Genuine Wood Grained Finish|14.95

productMaint2/web/WEB-INF/web.xml

ProductAdminController music.admin.ProductAdminController ProductAdminController /productMaint COOKIE 30 index.jsp