@ -48,6 +48,7 @@ import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND ;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND ;
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED ;
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED ;
import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_REPOSITORY ;
import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_REPOSITORY ;
import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT ;
import java.io.IOException ;
import java.io.IOException ;
import java.text.MessageFormat ;
import java.text.MessageFormat ;
@ -64,6 +65,7 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.errors.RepositoryNotFoundException ;
import org.eclipse.jgit.errors.RepositoryNotFoundException ;
import org.eclipse.jgit.lib.Repository ;
import org.eclipse.jgit.lib.Repository ;
import org.eclipse.jgit.transport.PacketLineOut ;
import org.eclipse.jgit.transport.resolver.RepositoryResolver ;
import org.eclipse.jgit.transport.resolver.RepositoryResolver ;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException ;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException ;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException ;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException ;
@ -131,14 +133,14 @@ public class RepositoryFilter implements Filter {
try {
try {
db = resolver . open ( req , name ) ;
db = resolver . open ( req , name ) ;
} catch ( RepositoryNotFoundException e ) {
} catch ( RepositoryNotFoundException e ) {
( ( HttpServletResponse ) rsp ) . sendError ( SC_NOT_FOUND ) ;
sendError ( SC_NOT_FOUND , req , ( HttpServletResponse ) rsp ) ;
return ;
} catch ( ServiceNotEnabledException e ) {
sendError ( SC_FORBIDDEN , req , ( HttpServletResponse ) rsp ) ;
return ;
return ;
} catch ( ServiceNotAuthorizedException e ) {
} catch ( ServiceNotAuthorizedException e ) {
( ( HttpServletResponse ) rsp ) . sendError ( SC_UNAUTHORIZED ) ;
( ( HttpServletResponse ) rsp ) . sendError ( SC_UNAUTHORIZED ) ;
return ;
return ;
} catch ( ServiceNotEnabledException e ) {
( ( HttpServletResponse ) rsp ) . sendError ( SC_FORBIDDEN ) ;
return ;
}
}
try {
try {
request . setAttribute ( ATTRIBUTE_REPOSITORY , db ) ;
request . setAttribute ( ATTRIBUTE_REPOSITORY , db ) ;
@ -148,4 +150,54 @@ public class RepositoryFilter implements Filter {
db . close ( ) ;
db . close ( ) ;
}
}
}
}
static void sendError ( int statusCode , HttpServletRequest req ,
HttpServletResponse rsp ) throws IOException {
String svc = req . getParameter ( "service" ) ;
String accept = req . getHeader ( HDR_ACCEPT ) ;
if ( svc ! = null & & svc . startsWith ( "git-" ) & & accept ! = null
& & accept . contains ( "application/x-" + svc + "-advertisement" ) ) {
// Smart HTTP service request, use an ERR response.
rsp . setContentType ( "application/x-" + svc + "-advertisement" ) ;
SmartOutputStream buf = new SmartOutputStream ( req , rsp ) ;
PacketLineOut out = new PacketLineOut ( buf ) ;
out . writeString ( "# service=" + svc + "\n" ) ;
out . end ( ) ;
out . writeString ( "ERR " + translate ( statusCode ) ) ;
buf . close ( ) ;
return ;
}
if ( accept ! = null & & accept . contains ( UploadPackServlet . RSP_TYPE ) ) {
// An upload-pack wants ACK or NAK, return ERR
// and the client will print this instead.
rsp . setContentType ( UploadPackServlet . RSP_TYPE ) ;
SmartOutputStream buf = new SmartOutputStream ( req , rsp ) ;
PacketLineOut out = new PacketLineOut ( buf ) ;
out . writeString ( "ERR " + translate ( statusCode ) ) ;
buf . close ( ) ;
return ;
}
// Otherwise fail with an HTTP error code instead of an
// application level message. This may not be as pretty
// of a result for the user, but its better than nothing.
//
rsp . sendError ( statusCode ) ;
}
private static String translate ( int statusCode ) {
switch ( statusCode ) {
case SC_NOT_FOUND :
return HttpServerText . get ( ) . repositoryNotFound ;
case SC_FORBIDDEN :
return HttpServerText . get ( ) . repositoryAccessForbidden ;
default :
return String . valueOf ( statusCode ) ;
}
}
}
}