Wednesday, July 24, 2013

Blank browser page - Error: The character encoding of the plain text document was not declared

I was browsing along fine on our financial calculator pages at www.calcxml.com using Firefox 22.0. All of a sudden, the calculator input pages stopped rendering. Just a blank page would be displayed. Here is the error message I found in the Firefox error console:


Error: The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature.

After googling the error and going through several suggestions, nothing seemed to help. I was already declaring the character encoding for the web pages. Long story short, after much troubleshooting, I figured out it was the cookie length. Each of our calculators can store a cookie with the previous values used as input. I had gone through about 20 of them when the error finally occurred. I'm not sure what cookie limitations there are, but the error appeared to occur somewhere between 7600 and 7800 characters. (The error occurred with 7786 characters.)

Saturday, February 09, 2013

org.xml.sax.SAXParseException: Invalid byte 1 of 1-byte UTF-8 sequence

I came across this error as I was trying to unmarshal some xml data that was being received by a REST service:

org.xml.sax.SAXParseException: Invalid byte 1 of 1-byte UTF-8 sequence

I would only get the error sporadically. I finally determined it was due to certain characters in the XML being passed in. When I changed the XML encoding of the incoming XML to ISO-8859-1, then one particular XML document would work fine, but I didn't want to have to worry about changing from UTF-8, as UTF-8 is pretty much the global standard now.

After digging a little deeper, I decided to change my unmarshal code from this:

            byte[] bArray = xmlInput.getBytes();
           ByteArrayInputStream bais = new ByteArrayInputStream(bArray);
            JAXBContext jc = JAXBContext.newInstance(Article.class);
            Unmarshaller u = jc.createUnmarshaller();
            Article article = (Article) u.unmarshal(bais);


to this:

            StringReader reader = new StringReader(xmlInput);
            JAXBContext jc = JAXBContext.newInstance(Article.class);
            Unmarshaller u = jc.createUnmarshaller();
            Article article = (Article) u.unmarshal(reader);


This solved my problems! Apparently the conversion of the xml into a byte array was causing the problem. My initial code was taken from a tutorial I had gone through so I hadn't considered that it would be causing the problems but glad I found it.

Tuesday, December 04, 2012

JSP RAT by Jeroy

I found some strange looking web apps in our Tomcat folder on an old machine that we no longer use. It looks like they had been deployed by dropping a war file into the webapps folder. Here are the war file names:

fLfTPWqT.war, HjnZlFSg.war, sRLNRtow.war

I believe the entry point was an unsecured copy of xampp. If you browsed to the IP address of the server, you would be presented with an xampp login dialog. The xampp username and password had not been changed since the original install, so I am assuming this is how the war files were installed.

I removed the web apps and fixed the vulnerability in the xampp install. I didn't notice anything else wrong with the server, but I guess time will tell.

I was curious what the installed web app's functionality was, so I installed it on a testing machine at the office. It's actually a pretty cool app, but of course, quite dangerous to have running on a production machine somewhere. It's pretty much a file manipulation app - browse files/directories, download/upload, delete, copy, rename, move and launch external programs.

Here is a screenshot:

JSP RAT screenshot
If you encounter problems by having had this app on your systems, please provide any help you can here for others.

Additional relevant information can be found here: http://grokbase.com/t/tomcat/users/12bv3sbkpy/malware-found-the-tomcat-6-0-29


Saturday, June 23, 2012

Jersey, REST, MyBatis

I am new to REST and Jersey. Have been going through some online tutorials I googled and reading some books. I created a project and got everything setup. I created the database layer using MyBatis, which I have used in many other projects. I followed the tutorial exactly and got it working. As soon as I used MyBatis to create and populate the bean/pojo object, I would get this exception message:

SEVERE: A message body writer for Java class com.myproject.dto.Person$$EnhancerByCGLIB$$3806666a, and Java type class com.myproject.dto.Person, and MIME media type text/xml was not found

Not knowing much about how Jersey worked, with all the annotations, I initially thought it was a problem with one of the data types of the Person class, but soon discovered that was not it. I created two Person objects, one through MyBatis and one using the Java new operator. I noticed the one created with the Java new operator had member variables named CGLIB$BOUND set to true and CGLIB$CALLBACK_0 set with a value, but the Person object created by MyBatis did not. I googled "cglib$bound in mybatis" and found an entry that took me to the MyBatis source code with some error text that said "Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath."

So I decided to disable lazy loading in my MyBatis config file

<setting name="lazyLoadingEnabled" value="false"/>

and this fixed the problem!

Is this documented somewhere? Did I really have to spend 3 hours trying to track this down?

Monday, July 25, 2011

Apache Server intermittently consumes all bandwidth

I noticed that my network would become very slow every few minutes. Sometimes RDP sessions would drop and reconnect. Sometimes messenger applications would drop and reconnect. Using some monitoring tools that come with my firewall appliance, I tracked it down to one server on the network. The server is a Windows 2000 server running an xampp installation of Apache Server 2.2 and Tomcat 6.0.20.

I noticed when I turned off Apache, the problem went away. Since I need Apache running though, this could not be the solution. After some reading, I decided to disable PHP. I commented out a couple of lines in a config file to disable it, but this did not fix it. I watched Windows Task Manager and noticed that php-cgi.exe would pop up when the bandwidth problem would occur. I researched php-cgi.exe and found that there are some issues with it running on Windows. I didn't investigate further to find out all the reasons, or even how to fix it because I don't need PHP running on my server. I renamed the php-cgi.exe file and the problem has gone away.

I suspect I could also have commented out more of the php settings in the config file, but renaming the exe file was sufficient.

Wednesday, July 06, 2011

301 Redirect in Struts

I had the need to do a 301 redirect in my Struts-based web application. I first tried using a redirect="true" attribute in struts-config.xml like this:

<action path="/oldPath" type="com.myapp.controller.MyAction" name="myForm" scope="request" validate="false" parameter="Dispatch">
<forward name="Success" redirect="true" path="/do/newPath">
</forward>

This worked but it turns out it does a 302 redirect (a temporary redirect) as opposed to a permanent redirect, which is a 301. I could not find a way to make this work. What I ended up doing was the following:

[struts-config.xml]
Modified the old action element to use a new MyRedirAction class.

<action path="/oldPath" type="com.myapp.controller.MyRedirAction" name="myForm" scope="request" validate="false" parameter="Dispatch">
</action>


The MyRedirAction class looks like this:

package com.calcxml.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.myStuff.base.ServiceException;

public class MyRedirAction extends MyBaseAction {

protected Map getKeyMethodMap() {
Map<string, string=""> map = new HashMap<string, string="">();
return map;
}

public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws ServiceException {
String qs = request.getQueryString();
response.setStatus(301);
if (qs != null) {
response.setHeader("Location", "newPath" + "?" + qs);
} else {
response.setHeader("Location", "newPath");
}
response.setHeader("Connection", "close" );
return null;
}
}



One gotcha that got me was that I assumed incorrectly that the query string (url parameters) would get passed along automatically in the redirect, but that was not the case. I had to add in the additional code to append the query string to the Location header variable.

Tuesday, April 12, 2011

prunsrv.c error failed creating java

I encounter this error on occasion while setting up servers. The time between each occurrence is just long enough that I forget how to fix it and have to do various google searches. Here is what I have done to fix this:

1. Copy msvcr71.dll from java's bin folder to tomcat's bin folder.
2. Add java's bin directory to windows PATH environment variable.
3. Copy msvcr71.dll from java's bin folder to windows\system32 folder.
4. Make sure your tomcat's pointing to correct jvm.dll folder.

On number 4, what I have done is I run tomcat\bin\tomcat6w.exe which brings up Apache Tomcat Properties dialog box. You can see the java settings on the Java tab. Make sure the Java Virtual Machine is pointing to the correct JVM.