Friday, October 17, 2014

Grails Spring Security no password encoding

http://stackoverflow.com/questions/8465016/spring-security-no-password-encoding

t's a really really bad idea in general, but if you have a use case for it then it is doable. Override the passwordEncoder bean in grails-app/conf/spring/resources.groovy:

import org.springframework.security.authentication.encoding.PlaintextPasswordEncoder

beans = {
   passwordEncoder(PlaintextPasswordEncoder)
}

Tuesday, October 14, 2014

MySQL Export Query Result to CSV / text file

SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

Saturday, October 11, 2014

MikroTik PPTP Server, client cannot access LAN devices

Don't forget to set up your interface for proxy-arp

Interfaces -> Ether2 ( your interface ) -> ARP : Proxy-arp

Wednesday, July 23, 2014

Jasper Applet in Grails

I want to view the report from web browser, something like :

First, download jasper report source project in this article I use jasper 5.6.0 and download from this link : http://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%205.6.0/jasperreports-5.6.0-project.zip/download After download extract the file and go to sample directory (jasperreports-5.6.0\demo\samples) from command prompt / shell terminal and compile using ant (ant.apache.org). To compile the sample just run : ant

Copy files from folder jasperreports-5.6.0\demo\samples\webapp\applets :
  • commons-collections-3.2.1.jar
  • commons-logging-1.1.1.jar
  • jasperreports-applet-5.6.0.jar
  • JRViewerSimple.class
  • EmbeddedViewerApplet.class
Copy to web-app\applets folder (in our grails project)

Create a controller that send an octet-stream, my controller : 

import javax.servlet.ServletOutputStream
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

class LaporanController {

 def dataSource
    

 def embedbarang() {

  String namaFile, reportName, dotJasper
  namaFile = "barang"

      reportName = grailsApplication.getMainContext().getResource('reports/' + namaFile + '.jrxml').file.getAbsoluteFile()
      dotJasper = grailsApplication.getMainContext().getResource('reports/' + namaFile + '.jasper').file.getAbsoluteFile()

  // compiles jrxml
  JasperCompileManager.compileReportToFile(reportName);
  // fills compiled report with parameters and a connection
  // JasperPrint print = JasperFillManager.fillReport(reportName + ".jasper", parameters, connection);
  JasperPrint print = JasperFillManager.fillReport(dotJasper, null, dataSource.getConnection());


  response.setContentType("application/octet-stream");
  ServletOutputStream outputStream = response.getOutputStream();
  
  ObjectOutputStream oos = new ObjectOutputStream(outputStream);
  oos.writeObject(print);
  oos.flush();
  oos.close();

  outputStream.flush();
  outputStream.close();

 }

}
Load the applet from .gsp file :

For copy-paste purpose :
<APPLET  CODE = "EmbeddedViewerApplet.class" codebase="${request.contextPath}/applets"  ARCHIVE = "jasperreports-applet-5.6.0.jar,commons-logging-1.1.1.jar,commons-collections-3.2.1.jar" WIDTH = "100%" HEIGHT = "600"></XMP>
    <PARAM NAME = CODE VALUE = "EmbeddedViewerApplet.class" >
    <PARAM NAME = CODEBASE VALUE = "${request.contextPath}/applets" >
    <PARAM NAME = ARCHIVE VALUE = "jasperreports-applet-5.6.0.jar,commons-logging-1.1.1.jar,commons-collections-2.1.1.jar" >

    <PARAM NAME="type" VALUE="application/x-java-applet;version=1.2.2">
    <PARAM NAME="scriptable" VALUE="false">
    <PARAM NAME = "REPORT_URL" VALUE ="${request.contextPath}/laporan/embedbarang">
</APPLET>



NB:

Sunday, July 20, 2014

Grails using jasper report without plugin

This snippet is used to generate .pdf file and the send the byte array to the browser, so the user can print the .pdf directly to the printer. I use jasper report to generate the .pdf file. My grails version is 2.4.2. First, download the necessary .jar file, I use this version of jar :

  • commons-beanutils-1.9.2.jar
  • commons-collections-3.2.jar
  • commons-digester-2.1.jar
  • commons-logging-1.2.jar
  • itext-2.1.7.jar
  • jasperreports-5.6.0.jar

You can download from this link : https://drive.google.com/file/d/0B3BD2OiyeQ3UQWN2Nk82R1dhYmM/edit?usp=sharing

After download the .jar files, put those .jar file to the /lib folder. Then I use this code in my controller :

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;

class LabController {

    def dataSource
    
    def print() {
     try {
      String reportName, namaFile, dotJasper
      namaFile = "barang"
      reportName = grailsApplication.mainContext.getResource('reports/' + namaFile + '.jrxml').file.getAbsoluteFile()
      dotJasper = grailsApplication.mainContext.getResource('reports/' + namaFile + '.jasper').file.getAbsoluteFile()

      // Report parameter
      Map reportParam = new HashMap()

      // compiles jrxml
      JasperCompileManager.compileReportToFile(reportName);
      // fills compiled report with parameters and a connection
      JasperPrint print = JasperFillManager.fillReport(dotJasper, reportParam, dataSource.getConnection());

      ByteArrayOutputStream  pdfStream = new ByteArrayOutputStream();

      // exports report to pdf
      JRExporter exporter = new JRPdfExporter();
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, pdfStream); // your output goes here
   
      exporter.exportReport();

     } catch (Exception e) {
   
      throw new RuntimeException("It's not possible to generate the pdf report.", e); 
     } finally {
      render(file: pdfStream.toByteArray(), contentType: 'application/pdf')
     }
    }
}

Thursday, June 26, 2014

Using netinstall to update MikroTik


  1. Download netinstall (http://www.mikrotik.com/download)
  2. Download the package (http://www.mikrotik.com/download)
  3. Run/Open netinstall
  4. Click "Net Booting" button
  5. Check "Boot Server Enabled"
  6. Fill client IP address with 192.168.88.10 (in this example my computer IP address is 192.168.88.9)
  7. Connect router board and PC via ether1
  8. Turn off the router board
  9. User paper clip or something to push the reset button on the router board
  10. Push and hold the reset button, turn on the router board, stay hold the reset button until the act indicator stop flashing
  11. Wait 10-20 seconds it should detected from netinstall
  12. Select the package and click install button

Wednesday, June 25, 2014

Grails Jasper plugins - direct pdf output

I use jasper plugin (http://grails.org/plugin/jasper) in my grails application. I want to send direct pdf file to the browser.

package com.jasper

import org.codehaus.groovy.grails.plugins.jasper.JasperExportFormat
import org.codehaus.groovy.grails.plugins.jasper.JasperReportDef

class LabController {

    def jasperService
    def directpdf() {

        def reportDef = new JasperReportDef(name:'mbarang.jrxml', fileFormat:JasperExportFormat.PDF_FORMAT)
        response.contentType = 'application/pdf'
        response.outputStream << jasperService.generateReport(reportDef).toByteArray()
        return(false);
    }
}

Tuesday, May 20, 2014

Using DOMPDF in Yii


  1. Download dompdf : https://code.google.com/p/dompdf/
  2. Extract and copy dompdf folder to protected\vendor
  3. Add the following command to the top of controller that will using dompdf
Yii::import('application.vendor.*');
require_once('dompdf/dompdf_config.inc.php');
Yii::registerAutoloader('DOMPDF_autoload');
  1. Then we should can used like this
 public function actionDompdf() {
...
  $pdf = new DOMPDF();
...
 }

I think we can use the same way to including/import another library

Monday, April 14, 2014

Reset mikrotik to factory default using reset button

1. turn off mikrotik, press and hold button reset (using paper clip or something)
2. apply power (plug the device power), and wait until the ACT LED starts flashing. Now release the button to clear configuration.

Note: If you wait until LED stops flashing, and only then release the button - this will instead launch Netinstall mode, to reinstall RouterOS.

Sunday, February 9, 2014

Java Play MP3 File

1. Download Java Media Framework (JMF) from : http://www.oracle.com/technetwork/java/javase/download-142937.html
2. Download MP3 Plugin from : http://www.oracle.com/technetwork/java/javase/download-137625.html or http://pscode.org/lib/mp3plugin.jar
3. Code for playing the mp3 file is like :


import javax.media.Manager;
import javax.media.Player;
import javax.media.Format;
import javax.media.MediaLocator;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;

...
        Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
        Format input2 = new AudioFormat(AudioFormat.MPEG);
        Format output = new AudioFormat(AudioFormat.LINEAR);

        Format[] formatIn = new Format[2];
        formatIn[0] = input1;
        formatIn[1] = input2;

        Format[] out = new Format[1];
        out[0] = output;

        File mp3file = new File("a.mp3");

        PlugInManager.addPlugIn(
            "com.sun.media.codec.audio.mp3.JavaDecoder",
            formatIn,
            out,
            PlugInManager.CODEC
        );
        try{
            Player player = Manager.createPlayer(new MediaLocator(mp3file.toURI().toURL()));
            player.start();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
...