Pages

Friday 9 December 2011

How to find latitude and longitude

The always helpful Tech-Recipes has come up with an ingenius way to find latitude and longitude values for any location using Google Maps.
You'll first need to look up an address (duh), but this trick only works if the address is centered (it's centered by default). So, moving the map around will not make this work. When the address you want to find latitude and longitude for is dead center, copy and paste this code into your browser bar:

javascript:void(prompt('',gApplication.getMap().getCenter()));

You'll get a popup with the coordinates. 

Get Latitude and Longitude values from Google Maps [Tech-Recipes ]

Friday 2 December 2011

Getting the Requesting URL in a Servlet

A servlet container breaks up the requesting URL into convenient components for the servlet. The standard API does not require the original requesting URL to be saved and therefore it is not possible to get the requesting URL exactly as the client sent it. However, a functional equivalent of the original URL can be constructed. The following example assumes the original requesting URL is:

public static String getUrl3(HttpServletRequest req) {
    String scheme = req.getScheme();             // http
    String serverName = req.getServerName();     // hostname.com
    int serverPort = req.getServerPort();        // 80
    String contextPath = req.getContextPath();   // /mywebapp
    String servletPath = req.getServletPath();   // /servlet/MyServlet
    String pathInfo = req.getPathInfo();         // /a/b;c=123
    String queryString = req.getQueryString();          // d=789

    // Reconstruct original requesting URL
    String url = scheme+"://"+serverName+":"+serverPort+contextPath+servletPath;
    if (pathInfo != null) {
        url += pathInfo;
    }
    if (queryString != null) {
        url += "?"+queryString;
    }
    return url;
}


http://www.exampledepot.com/egs/javax.servlet/GetReqUrl.html

 Origin url:
request.getHeader("Referer");

Monday 28 November 2011

ssh without password


Optional
Advanced

g Security implications, not recommended for everyone.
Prerequisit:
Install cygwin g on a Windows g machine.

On the g Windows machine, generate RSA (key-pair) public key and private key:
Double click on the cygwin icon g , a black screen pops up,
ssh-keygen -t  rsa
Enter file in which to save the key (/home/username/.ssh/id_rsa): just hit Enter
Enter passphrase (empty for no passphrase): just hit Enter
Here is a diagram of some of the possible topologies of your internal network.
Copy your public key to a Linux machine g with sshd running
scp  .ssh/id_rsa.pub  username@remotehost:~
(remotehost can be a host name or an ip address)  (substitute username with the actual user logon)
ssh  username@remotehost
mkdir  .ssh

If the file ~/.ssh/authorized_keys exists, you probably don't want to destroy that file, do:
cat  id_rsa.pub  >> .ssh/authorized_keys
If the file ~/.ssh/authorized_keys does not exist, do:
mv  id_rsa.pub   .ssh/authorized_keys
Copy your public key to a LRP box g with sshd running
If the remotebox has the file /root/.ssh/authorized_keys in existence, you probably don't want
to destroy it. In that case, do: (remotebox can be a host name or an ip address)
scp  .ssh/id_rsa.pub   root@remotebox:/root/.ssh/newkey
ssh  root@remotebox
cd   /root/.ssh
cat  newkey  >>  authorized_keys
 (this appends the newkey to the authorized_keys)
(If you like, make the keys permanent by typing lrcfg and backup root )
If the remotebox does not have file /root/.ssh/authorized_keys in existence:
(also make sure that on the LRP box,  /root/.ssh directory exists, if not, create one by doing this)
ssh  root@remotebox
cd   /root
mkdir .ssh

now copy the RSA public key to the LRP box:
scp  .ssh/id_rsa.pub  root@remotebox:/root/.ssh/authorized_keys
Test the RSA key-authentication scheme, from the g Windows machine:
ssh  username@remotehost  (to a Linux machine) or
ssh  root@remotehost   (to a LRP box)
(remotehost can be a host name, or an ip address)
You should be able to login to the remote without typing password.
pwd
shows you that you are in   /home/username (Linux box) or in  /root  (LRP box)
If above is not successful, check sshd configuration:
vi   /etc/ssh/sshd_config  (on the Linux box)
RSAAuthentication   yes
PubkeyAuthentication  yes
or
ae  /etc/ssh/sshd_config   (on the LRP box)
RSAAuthentication   yes
PubkeyAuthentication  yes

Disclaimer
© 2002-2004 Nicholas Fong
Last revision date:  September 13, 2004

Tuesday 27 September 2011

Convert Java Map to String


http://java.dzone.com/articles/two-ways-convert-java-map

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class MapUtil {
 public static String mapToString(Map<String, String> map) {
   StringBuilder stringBuilder = new StringBuilder();

   for (String key : map.keySet()) {
    if (stringBuilder.length() > 0) {
     stringBuilder.append("&");
    }
    String value = map.get(key);
    try {
     stringBuilder.append((key != null ? URLEncoder.encode(key, "UTF-8") : ""));
     stringBuilder.append("=");
     stringBuilder.append(value != null ? URLEncoder.encode(value, "UTF-8") : "");
    } catch (UnsupportedEncodingException e) {
     throw new RuntimeException("This method requires UTF-8 encoding support", e);
    }
   }

   return stringBuilder.toString();
  }

  public static Map<String, String> stringToMap(String input) {
   Map<String, String> map = new HashMap<String, String>();

   String[] nameValuePairs = input.split("&");
   for (String nameValuePair : nameValuePairs) {
    String[] nameValue = nameValuePair.split("=");
    try {
     map.put(URLDecoder.decode(nameValue[0], "UTF-8"), nameValue.length > 1 ? URLDecoder.decode(
     nameValue[1], "UTF-8") : "");
    } catch (UnsupportedEncodingException e) {
     throw new RuntimeException("This method requires UTF-8 encoding support", e);
    }
   }

   return map;
  }
}

Example usage

Map<String, String> map = new HashMap<String, String>();
map.put("color", "red");
map.put("symbols", "{,=&*?}");
map.put("empty", "");
String output = MapUtil.mapToString(map);
Map<String, String> parsedMap = MapUtil.stringToMap(output);
for (String key : map.keySet()) {
 Assert.assertEquals(parsedMap.get(key), map.get(key));
}

Monday 19 September 2011

Linux / Unix Command: rsync

Linux / Unix Command: rsync

rsync -avz foo:src/bar/ /data/tmp


this would recursively transfer all files from the directory src/bar on the machine foo into the /data/tmp/bar directory on the local machine. The files are transferred in "archive" mode, which ensures that symbolic links, devices, attributes, permissions, ownerships etc are preserved in the transfer. Additionally, compression will be used to reduce the size of data portions of the transfer.

ApacheMySQLPHP

Friday 9 September 2011

Eclipse hangs while loading workbench on Ubuntu

Eclipse was hanging on startup while loading the workbench. I tried various solutions and eventually got it to load by changing the version of java used

sudo update-alternatives --config java 

 Original stack trace was

Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:3884)
    at org.eclipse.swt.SWT.error(SWT.java:3799)
    at org.eclipse.swt.SWT.error(SWT.java:3770)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:450)
    at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:389)
    at org.eclipse.swt.widgets.Control.isVisible(Control.java:3168)
    at org.eclipse.swt.widgets.ProgressBar.timerProc(ProgressBar.java:276)
    at org.eclipse.swt.widgets.Display.windowTimerProc(Display.java:4209)
    at org.eclipse.equinox.launcher.JNIBridge._takedown_splash(Native Method)
    at org.eclipse.equinox.launcher.JNIBridge.takeDownSplash(JNIBridge.java:138)
    at org.eclipse.equinox.launcher.Main.takeDownSplash(Main.java:1940)
    at org.eclipse.equinox.launcher.Main$SplashHandler.run(Main.java:109)

Thursday 1 September 2011

Propeller Hat

This is Sean McMains' technical blog, where he writes about matters that would bore his wife too much to post on his personal site. Its focus is largely on information about Magnolia CMS, for which he went to work in March 2011.

Freemarker

FreeMarker is a "template engine"; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It's a Java package, a class library for Java programmers. It's not an application for end-users in itself, but something that programmers can embed into their products.
FreeMarker is designed to be practical for the generation of HTML Web pages, particularly by servlet-based applications following the MVC (Model View Controller) pattern. The idea behind using the MVC pattern for dynamic Web pages is that you separate the designers (HTML authors) from the programmers. Everybody works on what they are good at. Designers can change the appearance of a page without programmers having to change or recompile code, because the application logic (Java programs) and page design (FreeMarker templates) are separated. Templates do not become polluted with complex program fragments. This separation is useful even for projects where the programmer and the HTML page author is the same person, since it helps to keep the application clear and easily maintainable.
Although FreeMarker has some programming capabilities, it is not a full-blown programming language like PHP. Instead, Java programs prepare the data to be displayed (like issue SQL queries), and FreeMarker just generates textual pages that display the prepared data using templates.

Querying and Searching using JCR

Querying and Searching using JCR
The JCR API defines a way to query a repository for content that meets user-defined criteria. The JCR 2.0 API actually makes it possible for implementations to support multiple query languages, and the specification requires support for two languages: JCR-SQL2 and JCR-QOM. JCR 1.0 defined two other languages (XPath and JCR-SQL), though these languages were deprecated in JCR 2.0.

XPathJCR-SQL2
//*
SELECT * FROM [nt:base]
//element(*,my:type)
SELECT * FROM [my:type]
/jcr:root/nodes/element(*,my:type)
SELECT * FROM [my:type] 
WHERE PATH([my:type])> LIKE '/nodes/%'
 AND DEPTH([my:type]) = CAST(2 AS LONG)
/jcr:root/nodes//element(*,my:type)
SELECT * FROM [my:type] 
WHERE PATH([my:type]) LIKE '/nodes/%'
/jcr:root/nodes//element(ex:nodeName,my:type)
SELECT * FROM [my:type] 
WHERE PATH([my:type]) LIKE '/nodes/%'
 AND NAME([my:type]) = 'ex:nodeName'

Magnolia JCR Cheat Sheet

JCR 2.0 SQL-2 Grammar

These railroad diagrams are based on the JCR 2.0 specification.

examples from Jackrabbit's test cases:


Find pages using any paragraph in Magnolia

Wednesday 31 August 2011

RegEx

RegExr 0.3b, an intuitive tool for learning, writing, and testing Regular Expressions. Key features include:


Friday 26 August 2011

Attach Library Sources and Javadocs

Artifacts with sources deployed can be attached to Eclipse libraries using downloadSources. Javadocs may be attached using downloadJavadocs The sources and javadocs of the libraries must exist in the repository so that the plugin can download it and attach it.
The following example shows how to do this in the command-line:

mvn eclipse:eclipse -DdownloadSources=true  -DdownloadJavadocs=true
 
or in your pom.xml:
<project>
  [...]
  <build>
    [...]
    <plugins>
      [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.8</version>
        <configuration>
          <downloadSources>true</downloadSources>
          <downloadJavadocs>true</downloadJavadocs>
        </configuration>
      </plugin>
      [...]
    </plugins>
    [...]
  </build>
  [...]
</project>