package org.jboss.fresh.shell.commands.util;
import org.jboss.fresh.io.BufferObjectWriter;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.shell.AbstractExecutable;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
// EX OK
public class URLEncExe extends AbstractExecutable {
private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(URLEncExe.class);
public void process(String exepath, String[] params) throws Exception {
log.debug("entered");
if ((params.length == 0) || helpRequested()) {
PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
out.print("Usage: urlenc [--help] [-ex] [e|d] url\n");
out.print(" e : encodes the url taken from the pipeline and sends the encoded url to the pipeline.\n");
out.print(" d : decodes the url taken from the pipeline and sends the decoded url to the pipeline.\n");
out.print(" --help : this help\n");
out.close();
log.debug("done");
return;
}
PrintWriter err = new PrintWriter(new BufferWriter(getStdOut()));
try {
for (int i = 0; i < params.length; i++) {
String tmp = params[i];
if (tmp.equals("e")) {
// get command
String param = params[++i];
BufferObjectWriter bout = new BufferObjectWriter(getStdOut());
bout.writeObject(URLEncoder.encode(param, "utf-8"));
bout.close();
} else if (tmp.equals("d")) {
String param = params[++i];
BufferObjectWriter bout = new BufferObjectWriter(getStdOut());
bout.writeObject(URLDecoder.decode(param, "utf-8"));
bout.close();
} else {
if (canThrowEx()) {
throw new Exception("Unknown command: " + tmp);
} else {
err.println("Unknown command: " + tmp);
printUsage(err);
log.debug("done");
return;
}
}
}
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
if (canThrowEx()) {
throw ex;
} else {
err.println("Exception occured: " + ex.toString());
log.debug("done");
return;
}
}
log.debug("done");
}
public void printUsage(PrintWriter out) {
out.println(" Usage: ");
out.println(" urlenc e <text>");
out.println(" urlenc d <text>");
out.println();
}
}