Accessing Commission Junction’s API with Java

I couldn’t find a good example of how to connect to the CJ API with Java, so here is stripped down version of how I did it:

import java.net.*;
import java.io.*;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
 
...
 
public static Document getCommissionDetail(String startDate, String endDate){
    String response = "";
    String devKey = "YOUR-DEV-KEY";
    Document doc = null;
    String urlPath = "https://commission-detail.api.cj.com/v3/commissions?date-type=event&start-date=" + startDate + "&end-date=" + endDate;
 
      try {
        URL u = new URL(url);
        URLConnection uc = u.openConnection();
        HttpURLConnection connection = (HttpURLConnection) uc;
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Authorization", devKey) ;
        connection.setRequestProperty("Content-Type","text/xml");
        connection.setDoInput(true);
 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(connection.getInputStream());
        doc.getDocumentElement().normalize();
 
      }
      catch (IOException e) {
        //Something went wrong with the request
      }
      catch (Exception e) {
        //Something went wrong, very wrong
      }
    return doc;
}

Leave a comment if you find it useful or use it in a project!

HowTO: Draggabe JFrame Without Decoration

I was playing with writing a quick time tracking application called Efficiency which needed to be always ontop of other windows and therefore be a subtle as possible. I therefore decided to undecorate the window, however the user is then unable to reposition the window, so I wrote a some code to quickly allow them to do this by just dragging the JFrame.

My class extends the javax.swing.JFrame class, so I’ve set a few properties in the initComponents() function and more importantly added listeners for MousePressed and MouseDragged.

    private void initComponents() {
        ...
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(0, 0, 0));
        setResizable(false);
        setUndecorated(true);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });
        ...
    }

Firing an action on mousePressed is required to get the original location of the window. This fires the following function, setting the two variables which are of type java.awt.Point:

    private void formMousePressed(java.awt.event.MouseEvent evt) {
        this.start_drag = this.getScreenLocation(evt);
        this.start_loc = this.getLocation();
    }

The mouse dragged function is as follows:

    private void formMouseDragged(java.awt.event.MouseEvent evt) {
            Point current = this.getScreenLocation(evt);
    Point offset = new Point((int) current.getX() - (int) start_drag.getX(),
        (int) current.getY() - (int) start_drag.getY());

    Point new_location = new Point(
        (int) (this.start_loc.getX() + offset.getX()), (int) (this.start_loc
            .getY() + offset.getY()));
        this.setLocation(new_location);
    }

You’ll also need the utilty funciton to convert the points to a screen location:

      Point getScreenLocation(MouseEvent e) {
        Point cursor = e.getPoint();
        Point target_location = this.getLocationOnScreen();
        return new Point((int) (target_location.getX() + cursor.getX()),
            (int) (target_location.getY() + cursor.getY()));
      }

Reference

  • Drag and Move – material from java2s.com used as a basis but using an external MoveMouseListner class

Photo Credit: dongga BS