package de.saumya.mojo.rubygems;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

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

import org.sonatype.nexus.ruby.FileType;
import org.sonatype.nexus.ruby.GemArtifactFile;
import org.sonatype.nexus.ruby.RubygemsFile;
import org.sonatype.nexus.ruby.cuba.RubygemsFileSystem;
import org.sonatype.nexus.ruby.layout.Storage;

public class LegacyRubygemsServlet extends RubygemsServlet
{
    private static final long serialVersionUID = 2441264980328145654L;

    private static final String RUBYGEMS_S3_URL = "http://s3.amazonaws.com/production.s3.rubygems.org/gems/";
 
    @Override
    public void init() throws ServletException {
        super.init();
        
        this.fileSystem = (RubygemsFileSystem) getServletContext().getAttribute( RubygemsFileSystem.class.getName() );
        this.storage = (Storage) getServletContext().getAttribute( Storage.class.getName());
    }
    
    protected void handle( HttpServletRequest req, HttpServletResponse resp, RubygemsFile file ) 
            throws IOException, ServletException
    {
        if ( file.type() == FileType.GEM_ARTIFACT )
        {
            // use a non https url here since IVY can not handle
            // redirects from https to http
            // https://github.com/torquebox/rubygems-servlets/issues/11
            URL url = new URL("https://rubygems.org/gems/" + ((GemArtifactFile) file ).gem( null ).filename() + ".gem" );
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("HEAD");
            con.setInstanceFollowRedirects(false);
            String location = con.getHeaderField("Location");
            if (location == null) {
                // use the old hardcoded storage as fallback
                location = RUBYGEMS_S3_URL + ((GemArtifactFile) file ).gem( null ).filename() + ".gem";
            }
            resp.sendRedirect( location );
        }
        else
        {
            super.handle( req, resp, file );
        }
    }
}

@@ -684,26 +684,32 @@
 ng RUBYGEMS_
-S3
+DOWNLOAD
 _URL = %22http
@@ -715,39 +715,8 @@
 p://
-s3.amazonaws.com/production.s3.
 ruby
@@ -1939,10 +1939,16 @@
 EMS_
-S3
+DOWNLOAD
 _URL

Fix legacy servlet after rubygems.org stopped S3 redirects