package sagan.projects.support;

import sagan.projects.Project;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.databind.ObjectMapper;

import static org.springframework.web.bind.annotation.RequestMethod.*;

/**
 * Controller that handles ajax requests for project metadata, typically from the
 * individual Spring project pages managed via GitHub's "GH Pages" infrastructure at
 * http://projects.spring.io. See https://github.com/spring-projects/gh-pages#readme for
 * more information.
 */
@Controller
@RequestMapping("/project_metadata")
class ProjectMetadataController {

    private final ProjectMetadataService service;
    private final ObjectMapper objectMapper;

    @Autowired
    public ProjectMetadataController(ProjectMetadataService service, ObjectMapper objectMapper) {
        this.service = service;
        this.objectMapper = objectMapper;
    }

    @RequestMapping(value = "/{projectId}", method = { GET, HEAD })
    public void projectMetadata(@PathVariable("projectId") String projectId, @RequestParam("callback") String callback,
                                HttpServletResponse response) throws IOException {
        response.setContentType("text/javascript; charset=UTF-8");
        PrintWriter out = response.getWriter();

        Project project = service.getProject(projectId);

        out.print(callback + String.format("(%s);", this.objectMapper.writeValueAsString(project)));
    }

}

@@ -22,24 +22,69 @@
 s.support;%0A%0A
+import org.springframework.util.StringUtils;%0A
 import sagan
@@ -1435,16 +1435,48 @@
 ojectId,
+%0A                               
  @Reques
@@ -1482,16 +1482,22 @@
 stParam(
+value=
 %22callbac
@@ -1498,16 +1498,32 @@
 allback%22
+, required=false
 ) String
@@ -1786,24 +1786,180 @@
 rojectId);%0A%0A
+        if(callback == null %7C%7C !StringUtils.hasText(callback)) %7B%0A            out.print(this.objectMapper.writeValueAsString(project));%0A        %7D else %7B%0A    
         out.
@@ -2043,21 +2043,31 @@
 project)));%0A
+        %7D%0A
     %7D%0A%0A%7D%0A

Allow calls to metadata webservice without JSONP callback