/*
* Constellation - An open source and standard compliant SDI
* http://www.constellation-sdi.org
*
* Copyright 2014 Geomatys.
*
* 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 org.constellation.wps.ws.rs;
import org.apache.sis.util.logging.Logging;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A class to manage the file writing operation into request response messages.
*
* @author Alexis Manin (Geomatys)
* Date : 05/02/13
*/
public class FileWriter<T extends File> implements MessageBodyWriter<T> {
private static final Logger LOGGER = Logging.getLogger("org.constellation.wps.ws.rs");
@Override
public boolean isWriteable(final Class<?> type, final Type type1, final Annotation[] antns, final MediaType mt) {
return File.class.isAssignableFrom(type);
}
@Override
public long getSize(final T t, final Class<?> type, final Type type1, final Annotation[] antns, final MediaType mt) {
return t.length();
}
@Override
public void writeTo(final T t, final Class<?> type, final Type type1, final Annotation[] antns, final MediaType mt,
final MultivaluedMap<String, Object> mm, final OutputStream out) throws IOException, WebApplicationException {
FileInputStream in = new FileInputStream(t);
try {
final byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead); // write
}
} finally {
try {
in.close();
} catch (IOException e) {
LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
}
}
}
}