/*
 * DownloadClient Geodateninfrastruktur Bayern
 *
 * (c) 2016 GSt. GDI-BY (gdi.bayern.de)
 *
 * 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 de.bayern.gdi.utils;

import de.bayern.gdi.gui.Controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpRequestBase;

/**
 * File handler for HttpClient.
 */
public class FileResponseHandler implements ResponseHandler<Boolean> {



    private static Unauthorized unauthorized;

    /** Set the Unauthorized handler.
     * @param setUnauthorized The handler to set.
     */
    public static synchronized void setUnauthorized(
            Unauthorized setUnauthorized) {
        unauthorized = setUnauthorized;

    }

    private static synchronized void callUnauthorized() {
        if (unauthorized != null) {
            unauthorized.unauthorized();
        }
    }

    private static final Logger log
        = Logger.getLogger(FileResponseHandler.class.getName());

    private static final int BUF_SIZE = 4096;

    private File file;

    private HttpRequestBase request;

    private WrapInputStreamFactory wrapFactory;

    public FileResponseHandler() {
    }

    public FileResponseHandler(File file, HttpRequestBase request) {
        this(file, null, request);
    }

    public FileResponseHandler(File file, WrapInputStreamFactory wrapFactory,
            HttpRequestBase request) {
        this.file = file;
        this.wrapFactory = wrapFactory;
        this.request = request;
    }

    private InputStream wrap(InputStream in) {
        return this.wrapFactory != null
            ? this.wrapFactory.wrap(in)
            : in;
    }

    @Override
    public Boolean handleResponse(HttpResponse response)
        throws IOException {
        int status = response.getStatusLine().getStatusCode();
        String reason = response.getStatusLine().getReasonPhrase();
        Controller.logToAppLog(status + " " + reason + " "
                + request.toString());
        if (status < HttpStatus.SC_OK
            || status >= HttpStatus.SC_MULTIPLE_CHOICES) {
            if (status == HttpStatus.SC_UNAUTHORIZED) {
                callUnauthorized();
            }
            throw new ClientProtocolException("Unexpected response status: "
                    + status);
        }
        HttpEntity entity = response.getEntity();
        if (entity == null) {
            return Boolean.FALSE;
        }
        try {
            try (FileOutputStream out = new FileOutputStream(this.file)) {
                byte [] buf = new byte[BUF_SIZE];
                int r;
                InputStream ins = wrap(entity.getContent());
                while ((r = ins.read(buf)) >= 0) {
                    out.write(buf, 0, r);
                }
                out.flush();
            }
        } catch (IOException ioe) {
            // Clean up debris if there was an error.
            // XXX: Maybe keep it to recover download?
            if (!this.file.delete()) {
                log.log(
                    Level.WARNING, "Deleting file '" + file + "' failed.");
            }
            throw ioe;
        }
        return Boolean.TRUE;
    }
}

@@ -713,24 +713,53 @@
 ontroller;%0A%0A
+import java.nio.file.Files;%0A%0A
 import java.
@@ -3842,34 +3842,48 @@
     
-if (!this.file.delete()) %7B
+deleteGraceful();%0A            throw ioe;
 %0A   
@@ -3887,24 +3887,26 @@
         
+%7D%0A
         
 log.log(
@@ -3901,16 +3901,71 @@
     
-log.log(
+return Boolean.TRUE;%0A    %7D%0A%0A    private void deleteGraceful() %7B
 %0A   
@@ -3961,32 +3961,38 @@
 ful() %7B%0A        
+try %7B%0A
             Leve
@@ -3991,100 +3991,166 @@
     
-Level.WARNING, %22Deleting file '%22 + file + %22' failed.%22);%0A            %7D%0A            throw ioe;
+Files.delete(this.file.toPath());%0A        %7D catch (IOException ioe) %7B%0A            log.log(%0A                Level.WARNING,%0A                () -%3E String.format(
 %0A   
@@ -4150,34 +4150,32 @@
 at(%0A        
-%7D%0A
         
 return Boole
@@ -4162,36 +4162,75 @@
         
-return Boolean.TRUE;
+    %22Deleting file %7B0%7D failed: %7B1%7D%22, file, ioe));%0A        %7D
 %0A    %7D%0A%7D

Use nio.file.Files.delete() instead of io.File.delete().