Categories
Typo3

TYPO3: Getting content from another page

This is a TYPO3 code snippet that will replace a marker with content from a specific page. I use this for common content on footers and outer columns.

page.10.marks.FOOTER < styles.content.get
page.10.marks.FOOTER {
      select.where = colPos=3
      select.pidInList = 2
}

colPos=3 will get the content from the border column (0 is center, 1 is left, 2 is right column). pidInList identifies the page (or pages) that you want to fetch the content from.

Categories
Typo3

Replacing TYPO3 Marker by Page Title

This short TYPO3 script snippet will replace a marker in your template by the current page’s title:

page.10.marks.MYMARKER = TEXT
page.10.marks.MYMARKER.field = title
Categories
Java

Hashing Passwords

Here is some code that you can use to hash passwords or other secrets in Java. I usually prefer to have such methods in a separate utility class:

protected static MessageDigest getDigest() throws NoSuchAlgorithmException {
	if (digest == null) {
		digest = MessageDigest.getInstance(&qout;MD5&qout;);
	}
	return digest;
}
 
public static byte[] digestString(String s) {
	if (s == null) return null;
	try {
		MessageDigest digest = getDigest();
		digest.update(s.getBytes());
		return digest.digest();
	} catch (Exception e) {
		log.error(&qout;Digesting problem:&qout;, e);
	}
	return null;
}
 
public static String encodePassword(String s) {
	byte b[] = digestString(s);
	if (b == null) return null;
	String rc = new String(Base64.encodeBase64(b));
	if (rc.length() &gt; 50) rc = rc.substring(0, 50);
	return rc;
}

Use the function encodePassword() to hash your string. Please note that the hash value is limited to a length of 50 characters.

Categories
Java

Configuring logging in a Java web application

Here is a short HOWTO for making some initial configuring when using Commons logging or log4j. Additionally this post will describe how to set the default locale in a servlet environment.

First you will need to create a new class derived from HttpServlet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package mypackage;
 
import java.util.Locale;
 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;
 
/**
 * Initializes log4j so it reads its configuration from WEB-INF directory and sets default locale.
 * @author Ralph Schuster
 *
 */
public class LogConfiguratorServlet extends HttpServlet {
 
   /**
    * Serial ID.
    */
   private static final long serialVersionUID = -5756062055681369027L;
 
   private static final String DEFAULT_FILE = "WEB-INF/log4j.properties";
 
   private static final String DEFAULT_LOCALE = "en_US"; // You can use "en", too.
 
   /**
    * Initializes log4j and sets default locale.
    */
   public void init() {
      // Do the log4j configuration
      String prefix =  getServletContext().getRealPath("/");
      String file = getInitParameter("config-file");
      // if the config-file is not set, then no point in trying
      String s = null;
      if (file != null) {
         s = prefix+file;
      } else {
         s = prefix+DEFAULT_FILE;
      }
      PropertyConfigurator.configure(s);
      LogFactory.getLog(getClass()).debug("log4j configuration file: "+s);
 
      // Do the locale configuration
      s = getInitParameter("locale");
      if (s == null) s = DEFAULT_LOCALE;
      Locale available[] = Locale.getAvailableLocales();
      for (int i=0; i&lt;available.length; i++) {
         if (available[i].toString().equals(s)) {
            Locale.setDefault(available[i]);
         }
      }
      LogFactory.getLog(getClass()).debug("Default locale set to: "+Locale.getDefault());
   }
 
   /**
    * Does nothing.
    */
   public void doGet(HttpServletRequest req, HttpServletResponse res) {
   }
 
}

You then need to deploy the class in your servlet container and adjust the web.xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   &lt;servlet&gt;
      &lt;/servlet-name&gt;log4j-init&lt;/servlet-name&gt;
      &lt;servlet-class&gt;mypackage.LogConfiguratorServlet&lt;/servlet-class&gt;
 
      &lt;init-param&gt;
         &lt;param-name&gt;config-file&lt;/param-name&gt;
         &lt;param-value&gt;WEB-INF/log4j.properties&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
         &lt;param-name&gt;locale&lt;/param-name&gt;
         &lt;param-value&gt;de_DE&lt;/param-value&gt;
      &lt;/init-param&gt;
 
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
   &lt;/servlet&gt;