blob: dc8b9cc9c65a01a59b40a190dab8f3ccbbbd453d [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008 Innoopract Informationssysteme GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innoopract Informationssysteme GmbH - initial API and implementation
******************************************************************************/
package org.eclipse.epp.wizard.jnlpbuilder;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.epp.wizard.installerbuilder.Activator;
import org.eclipse.epp.wizard.installerbuilder.BuildInstaller;
import org.eclipse.epp.wizard.util.StringUtil;
/**
* @author Jordi Boehme Lopez <jboehme@innoopract.com>
*/
public class CustomJnlp extends HttpServlet {
// public static final String OUT_PATH = "/opt/epp/htdocs";
// public static final String OUT_PATH = "C:/opt/epp/htdocs";
public static final String JNLP_TEMPLATE_PATH = "templates/jnlp/epp.jnlp";
private static final String ENCODING = "UTF-8";
private static final int BUFFER = 512;
private static final long serialVersionUID = 1L;
@Override
protected void doPost( final HttpServletRequest req,
final HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doGet( final HttpServletRequest req,
final HttpServletResponse resp )
throws ServletException, IOException
{
String codebase = "http://"
+ req.getServerName()
+ ":"
+ req.getServerPort()
+ "/download";
String iuParam = req.getParameter( "eclipse.p2.roots" );
String metadata = req.getParameter( "eclipse.p2.metadata" );
String artifacts = req.getParameter( "eclipse.p2.artifacts" );
resp.addHeader( "Content-Type", "application/x-java-jnlp-file" );
IPath jnlp_file = execute( codebase, iuParam, metadata, artifacts );
resp.flushBuffer();
ServletOutputStream os = resp.getOutputStream();
copy( new FileInputStream(jnlp_file.toFile()) , os);
os.flush();
}
public static IPath execute( final String codebase,
final String iuParam,
final String metadata,
final String artifacts ) throws IOException
{
File downloadDir = Activator.getDefault()
.getConfiguration()
.getDownloadsDirectory();
IPath outDirPath = new Path( new File( downloadDir,
BuildInstaller.OUTPUT_PATH ).getAbsolutePath() );
String[] ius = getIUs( iuParam );
String filename = getFileName( ius );
IPath outFilePath = outDirPath.append( filename );
File templateFile = new File( downloadDir, JNLP_TEMPLATE_PATH );
String jnlp = ceateCustomJnlp( templateFile.toURI().toURL(),
ius,
codebase,
filename,
metadata,
artifacts );
OutputStream os = new FileOutputStream( outFilePath.toFile() );
os.write( jnlp.getBytes( ENCODING ) );
tryClose( os );
return outFilePath;
}
// ////////////////
// helping methods
// ////////////////
private static String[] getIUs( final String param ) {
List<String> result = new ArrayList<String>();
StringTokenizer tknizr = new StringTokenizer( param, ",", false );
while( tknizr.hasMoreTokens() ) {
String token = tknizr.nextToken();
result.add( token );
}
return result.toArray( new String[ result.size() ] );
}
private static String ceateCustomJnlp( final URL template,
final String ius[],
final String codebase,
final String filename,
final String metadata,
final String artifacts )
{
String result = "";
InputStream in = null;
try {
in = template.openStream();
ByteArrayOutputStream bufferStream = new ByteArrayOutputStream();
copy( in, bufferStream );
result = customize( bufferStream.toString( ENCODING ),
ius,
codebase,
filename,
metadata,
artifacts );
} catch( IOException ioe ) {
ioe.printStackTrace();
} finally {
tryClose( in );
}
return result;
}
private static String customize( final String input,
final String[] ius,
final String codebase,
final String href,
final String metadata,
final String artifacts )
{
String result = input;
result = result.replaceAll( "\\$\\{codebase\\}", codebase );
result = result.replaceAll( "\\$\\{href\\}", href );
result = result.replaceAll( "\\$\\{roots\\}",
StringUtil.toCommaList( ius, false ) );
result = result.replaceAll( "\\$\\{metadata\\}", metadata );
result = result.replaceAll( "\\$\\{artifacts\\}", artifacts );
// FIXME : (jb) get this from initial selection on eclispe.org
result = result.replaceAll( "\\$\\{profileName\\}", "Custom Eclipse" );
return result;
}
private static void tryClose( final InputStream stream ) {
if( stream != null ) {
try {
stream.close();
} catch( IOException e ) {
e.printStackTrace();
// TODO Activator.log( e );
}
}
}
private static void tryClose( final OutputStream stream ) {
if( stream != null ) {
try {
stream.close();
} catch( IOException e ) {
e.printStackTrace();
// TODO Activator.log( e );
}
}
}
private static void copy( final InputStream in, final OutputStream out )
throws IOException
{
byte[] data = new byte[ BUFFER ];
int currentByte = in.read( data, 0, BUFFER );
while( currentByte != -1 ) {
out.write( data, 0, currentByte );
currentByte = in.read( data, 0, BUFFER );
}
}
public static String getFileName( final String[] ids ) {
String hash = ids.toString();
try {
hash = StringUtil.getHash( ids );
} catch( NoSuchAlgorithmException exc ) {
// should not happen! - but trace anyway
exc.printStackTrace();
}
return "startinstaller-" + hash + ".jnlp";
}
}