It's that time of the year again and, since it appears that the Maya spared us, I want to share with you a couple of gists that I came up with recently that could be generally useful. Btw, there are lots of other gists on my
gist.github.com profile, check them out.
If you find these script useful star them on github, drop me a comment or just share them. Once again, Merry Christmas everyone!
The first one is for Java people and is a HttpServletRequestWrapper that supports:
injection of the principal: for those cases when you use trust authentication and you are rolling your own SSO solution and/or you need to integrate with an existing SSO solution (I used it with for CAS )
supports reading of the InputStream multiple times. We all know that in a POST the request input stream can only be read once, so this will definitely help you if you need to access the post body or a request parameter in a Filter and make sure the upstream servlets/filters still work
File: AwesomeRequestWrapper.java
--------------------------------
package java.is.awesome; // joking
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import javax.servlet.ServletInputStream;
import javax.servlet.http.*;
import java.io.*;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;
/**
* AwesomeRequestWrapper adds two important features:
*
* 1. allow to set the user principal on the request so that the CAS can trust the user id
* 2. cache post data and parse them offline so that inpustream can be read multiple times
*/
public class AwesomeRequestWrapper extends HttpServletRequestWrapper {
Logger logger;
Principal awesomePrincipal = null;
Map parameters = null;
byte[] postData = null;
public AwesomeRequestWrapper(HttpServletRequest request) {
super(request);
logger = Logger.getLogger(this.getClass());
parameters = new HashMap();
}
public Principal getUserPrincipal() {
if (awesomePrincipal == null) {
return super.getUserPrincipal();
}
return awesomePrincipal;
}
public String getRemoteUser() {
if (awesomePrincipal == null) {
return super.getRemoteUser();
}
return awesomePrincipal.getName();
}
void setUserPrincipal(Principal p) {
awesomePrincipal = p;
}
@Override
public String getParameter(String name) {
String value = super.getParameter(name);
if (value == null) {
value = (String) parameters.get(name);
}
return value;
}
@Override
public ServletInputStream getInputStream() throws IOException {
logger.trace("called getInputStream");
if (postData == null) {
postData = IOUtils.toByteArray(super.getInputStream());
parameters = getQueryMap(new String(postData));
}
logger.trace("post data read, parsed and cached: " + new String(postData));
return new BAServletInputStream(new ByteArrayInputStream(postData));
}
private class BAServletInputStream extends ServletInputStream {
InputStream is;
BAServletInputStream(InputStream is) {
this.is = is;
}
@Override
public int read() throws IOException {
return is.read();
}
@Override
public int readLine(byte[] b, int off, int len) throws IOException {
return is.read(b, off, len);
}
@Override
public int read(byte[] bytes) throws IOException {
return is.read(bytes);
}
@Override
public int read(byte[] bytes, int i, int i1) throws IOException {
return is.read(bytes, i, i1);
}
@Override
public long skip(long l) throws IOException {
return is.skip(l);
}
}
/* this could actually be improved, there should be a method that does the same in Spring */
public Map<String, String> getQueryMap(String query) {
String[] params = query.split("&");
Map<String, String> map = new HashMap<String, String>();
for (String param : params) {
try {
String name = URIUtil.decode(param.split("=")[0]);
String value = URIUtil.decode(param.split("=")[1]);
map.put(name, value);
} catch (Exception e) {
logger.error("Cannot decode request parameter: " + e.getMessage());
}
}
return map;
}
}
The second
gift gist is for Windows admins and is written in vbs (I even do VBS when it is necessary, now you get my twitter
handle , don't you?). It is a login script that can be used in a Windows Domain to recreate Desktop links on each user logon. The configuration for each link is stored in the script as a dictionary of dictionaries and link-to-user assignment is done by adding the user to an AD group. The source is heavily commented and should be easy enough to understand for anyone who's ever programmed, even if not in vbs.
File: desktoplinksmanager.vbs
-----------------------------
Set FSO = CreateObject("scripting.filesystemobject")
Set objShell = WScript.CreateObject("WScript.Shell")
strDesktop = objShell.SpecialFolders("Desktop")
' Could you believe this is a comment? I know, vbs sucks
'
' The following is a dictionary of dictionaries.
' The first dictionary is keyed by group name (case sensitive!)
' while the second holds the attributes for the link to be created.
' With the example cfg below the script will create a link on the desktop
' of all users in the app_NOTEPAD group.
'
' To add a new link duplicate lines 22-28 and customize to taste
' Remember: options are all required (or improve this script so that)
' some can actually be left empty
'
' After that create a new group with the same name as the key
' you specified at line 28
'
Set shortcuts = CreateObject("Scripting.Dictionary")
Set app = CreateObject("Scripting.Dictionary")
app.Add "nome", "NOTEPAD.lnk"
app.Add "arguments", ""
app.Add "targetpath", "%windir%\system32\notepad.exe"
app.Add "workingdirectory", "%windir%\system32\notepad.exe"
app.Add "icon", "%windir%\system32\notepad.exe"
shortcuts.Add "app_NOTEPAD", app
Set oMember = GetObject("WinNT://"+objShell.ExpandEnvironmentStrings("%UserDomain%")+"/" + objShell.ExpandEnvironmentStrings("%UserName%"))
For Each oGroup in oMember.Groups
wscript.echo oGroup.Name
If shortcuts.Exists(oGroup.Name) Then
wscript.echo " Trovato shortcut: " & oGroup.Name
Set scut = shortcuts.Item(oGroup.Name)
If FSO.FileExists(strDesktop & "\" & scut.Item("nome")) = True Then
FSO.GetFile( strDesktop & "\" & scut.Item("nome") ).Delete(True)
End If
Set oMyShortcut = objShell.CreateShortcut( strDesktop & "\" & scut.Item("nome") )
oMyShortcut.WindowStyle = 3 'Maximized 7=Minimized 4=Normal
oMyShortcut.IconLocation = scut.Item("icon")
oMyShortcut.Arguments = scut.Item("arguments")
oMyShortcut.TargetPath = scut.Item("targetpath")
oMyShortcut.WorkingDirectory = scut.Item("workingdirectory")
oMyShortCut.Save
End If
Next