Java tutorial
/* * Copyright 2014 Guillaume Bailleul * * 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 net.gbmb.collector; import net.gbmb.collector.impl.TestFinalStorage; import net.gbmb.collector.impl.TestTempStorage; import org.apache.commons.io.IOUtils; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.io.ByteArrayOutputStream; import java.io.IOException; @RestController public class WrappingApplicationRunner extends ApplicationRunner { @Resource private FinalStorage finalStorage; @Resource private PushListenerWorker pushListenerWorker; @Override @Bean public TemporaryStorage temporaryStorage() { return new TestTempStorage(); } @Override @Bean public FinalStorage finalStorage() { return new TestFinalStorage(); } @RequestMapping(value = "/test/final/{cid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public byte[] getFinal(@PathVariable("cid") String cid) throws IOException { // ensure there is no queued content pushListenerWorker.wake(); // retrieve content try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(finalStorage.get(cid), bos); IOUtils.closeQuietly(bos); return bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); throw e; } catch (RuntimeException e) { e.printStackTrace(); throw e; } } }