oracle.otnsamples.cmsxdb.admin.Externalizer (Java2HTML)
package oracle.otnsamples.cmsxdb.admin;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import oracle.otnsamples.cmsxdb.ConnParams;
import oracle.otnsamples.cmsxdb.exception.SiteGenerationException;
public class Externalizer {
private String rootPath = null;
public Externalizer(String rootpath ) {
this.rootPath = rootpath;
}
public void generateFile( String resourceUrl, String xslLoc,String contentType)
throws SiteGenerationException {
HttpURLConnection urlConn = null;
String userPassword = ConnParams.dbUsername + ":" + ConnParams.dbPassword;
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
String filePath = this.rootPath + resourceUrl;
StringBuffer servletUrl = null;
if( contentType == null || "".equals(contentType) ) {
contentType = "text/xml";
}
try {
if( resourceUrl.toLowerCase().endsWith(".xml") ) {
servletUrl = new StringBuffer(ConnParams.dbTransServlet)
.append("?resource=")
.append(URLEncoder.encode(resourceUrl,"UTF-8"));
if( xslLoc != null && !"".equals(xslLoc) ) {
servletUrl.append( "&transform=" ).append(URLEncoder.encode(xslLoc,"UTF-8"));
}
servletUrl.append( "&contenttype=" ).append( contentType );
} else {
servletUrl = new StringBuffer(ConnParams.dbWebURL).append('/')
.append(URLEncoder.encode(resourceUrl.substring(1),"UTF-8"));
}
if( filePath.lastIndexOf('.') != -1 ) {
filePath = filePath.substring( 0, filePath.lastIndexOf('.') + 1 ) +
this.getFileExtn( contentType );
}
URL url = new URL( servletUrl.toString( ) );
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(false);
urlConn.setUseCaches(false);
urlConn.setDefaultUseCaches(false);
urlConn.setRequestProperty ("Authorization", "Basic " + encoding);
urlConn.connect();
int buffsize = 1 * 1024; byte buffer[] = new byte[buffsize];
int len = 0;
BufferedInputStream servletin = new BufferedInputStream(urlConn.getInputStream());
File dir = new File( filePath.substring(0, filePath.lastIndexOf('/') ) );
dir.mkdirs( );
FileOutputStream fileout = new FileOutputStream( filePath );
while( (len = servletin.read( buffer,0, buffsize ) ) != -1 ) {
fileout.write( buffer, 0, len );
}
servletin.close();
fileout.close();
} catch( MalformedURLException invalidURLEx) {
throw new SiteGenerationException(" Invalid Servlet URL to generate site : "+
invalidURLEx.getMessage());
} catch( IOException ioEx ) {
throw new SiteGenerationException(" Could not generate file : "+
ioEx.getMessage() );
} finally {
urlConn = null;
}
}
private String getFileExtn(String cType ) {
String extn = null;
if( cType.equals("application/msword") )
extn = "doc";
else if ( cType.equals("text/plain") )
extn = "txt";
else if ( cType.equals("image/jpeg") )
extn = "jpg";
else
extn = cType.substring( cType.indexOf( '/') + 1 );
return extn;
}
}
|