001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.infra.server.client.servlets;
038:
039: import java.io.File;
040: import java.io.IOException;
041: import java.io.OutputStream;
042: import java.io.PrintStream;
043: import java.io.RandomAccessFile;
044: import java.util.regex.Matcher;
045: import java.util.regex.Pattern;
046: import javax.servlet.http.HttpServletRequest;
047: import javax.servlet.http.HttpServletResponse;
048: import org.netbeans.installer.utils.StreamUtils;
049:
050: public final class Utils {
051: public static void transfer(HttpServletRequest request,
052: HttpServletResponse response, OutputStream output, File file)
053: throws IOException {
054: RandomAccessFile input = null;
055: try {
056: input = new RandomAccessFile(file, "r");
057:
058: final String range = request.getHeader("Range");
059: if (range == null) {
060: response.setStatus(HttpServletResponse.SC_OK);
061:
062: input.seek(0);
063:
064: response.setHeader("Content-Length", Long.toString(file
065: .length()));
066:
067: StreamUtils.transferData(input, output);
068: } else {
069: Matcher matcher = Pattern.compile(
070: "^bytes=([0-9]*)-([0-9]*)$").matcher(range);
071:
072: if (!matcher.find()) {
073: response
074: .setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
075: return;
076: } else {
077: long start = -1;
078: long finish = -1;
079:
080: if (!matcher.group(1).equals("")) {
081: start = Long.parseLong(matcher.group(1));
082: }
083:
084: if (!matcher.group(2).equals("")) {
085: finish = Long.parseLong(matcher.group(2));
086: }
087:
088: if ((start != -1)
089: && (finish != -1)
090: && ((start > finish) || (finish > file
091: .length()))) {
092: response
093: .setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
094: return;
095: }
096:
097: response
098: .setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
099:
100: if (start == -1) {
101: start = file.length() - finish;
102: finish = -1;
103: }
104:
105: input.seek(start);
106:
107: long length = (finish == -1 ? file.length() - start
108: : finish - start) + 1;
109:
110: response.setHeader("Content-Length", Long
111: .toString(length));
112: response.setHeader("Content-Range", "bytes "
113: + start
114: + "-"
115: + (finish == -1 ? file.length() - 1
116: : finish) + "/" + file.length());
117:
118: if (finish == -1) {
119: StreamUtils.transferData(input, output);
120: } else {
121: StreamUtils.transferData(input, output, finish
122: - start + 1);
123: }
124: }
125: }
126: } catch (IOException e) {
127: response
128: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
129: e.printStackTrace();
130: e.printStackTrace(new PrintStream(output));
131: } finally {
132: if (input != null) {
133: input.close();
134: }
135: }
136: }
137:
138: }
|