/*
 * Copyright 2016 zhanhb.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.zhanhb.download.spring;

import com.github.zhanhb.ckfinder.download.ContentDisposition;
import com.github.zhanhb.ckfinder.download.PathPartial;
import java.nio.file.Path;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 *
 * @author zhanhb
 */
public class PathDownloaderHandler implements HandlerMethodReturnValueHandler {

    private final PathPartial viewer = PathPartial.builder()
            .contentDisposition(ContentDisposition.inline())
            .build();
    private final PathPartial downloader = PathPartial.builder()
            .contentDisposition(ContentDisposition.attachment())
            .build();

    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return returnType.getMethodAnnotation(ToDownload.class) != null
                && Path.class.isAssignableFrom(
                        returnType.getMethod().getReturnType());
    }

    @Override
    public void handleReturnValue(Object returnValue,
            MethodParameter returnType,
            ModelAndViewContainer mavContainer,
            NativeWebRequest webRequest) throws Exception {
        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
        ToDownload toDownload = returnType.getMethodAnnotation(ToDownload.class);

        PathPartial d = toDownload.attachment() ? downloader : viewer;
        d.service(request, response, (Path) returnValue);
        mavContainer.setRequestHandled(true);
    }

}

@@ -748,16 +748,49 @@
 artial;%0A
+import java.lang.reflect.Method;%0A
 import j
@@ -1665,15 +1665,12 @@
     
-return 
+if (
 retu
@@ -1721,16 +1721,19 @@
  != null
+) %7B
 %0A       
@@ -1741,14 +1741,104 @@
     
-    &&
+Method method = returnType.getMethod();%0A            if (method != null) %7B%0A                return
  Pat
@@ -1862,16 +1862,40 @@
 bleFrom(
+method.getReturnType());
 %0A       
@@ -1891,36 +1891,44 @@
 ));%0A            
+%7D%0A
     
+    %7D%0A
         returnTy
@@ -1929,41 +1929,14 @@
 turn
-Type.getMethod().getReturnType())
+ false
 ;%0A  
@@ -2423,24 +2423,58 @@
 ad.class);%0A%0A
+        @SuppressWarnings(%22null%22)%0A
         Path

Fix null pointer dereference