Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.kristofa.test.http.file;
  
  import java.io.File;
  import java.util.List;
 
 
Builds a HttpRequest for which the content is stored on disk. It can reconstruct a HttpRequest which was previously stored with HttpRequestFileWriterImpl.

Author(s):
kristof
See also:
HttpRequestFileWriterImpl
 
 
     private static final String QUERY_PARAMETERS = "[QueryParameters]";
     private static final String PATH = "[Path]";
     private static final String HTTP_MESSAGE_HEADER = "[HttpMessageHeader]";
     private static final String METHOD = "[Method]";

    
 
     @Override
     public HttpRequest read(final File httpRequestFilefinal File httpRequestEntityFile) {
         final HttpRequestImpl httpRequestImpl = new HttpRequestImpl();
         try {
             readRequest(httpRequestImplhttpRequestFilehttpRequestEntityFile);
         } catch (final IOException e) {
             throw new IllegalStateException(e);
         }
         return httpRequestImpl;
     }
 
     private void readRequest(final HttpRequestImpl requestfinal File requestFilefinal File requestEntityFile)
         throws IOException {
         final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(requestFile), "UTF-8"));
         try {
             readNextLine(reader);
             final String method = reader.readLine();
             request.method(Method.valueOf(method));
             readNextLine(reader);
             final List<KeyValuePairhttpMessageHeaderValues = new ArrayList<KeyValuePair>();
             final String nextSection = readKeyValuePairs(readerhttpMessageHeaderValues);
             if (!.equals(nextSection)) {
                 throw new IllegalStateException("Expected " +  + " after " +  + " but got "
                     + nextSection);
             }
             for (final KeyValuePair keyValuePair : httpMessageHeaderValues) {
                 request.httpMessageHeader(keyValuePair.keykeyValuePair.value);
             }
             final String path = reader.readLine();
             request.path(path);
             readNextLine(reader);
             final List<KeyValuePairqueryParams = new ArrayList<KeyValuePair>();
             final String nextSection2 = readKeyValuePairs(readerqueryParams);
             if (nextSection2 != null) {
                 throw new IllegalStateException("Expected nothing after " +  + " but got " + nextSection2);
             }
             for (final KeyValuePair keyValuePair : queryParams) {
                 request.queryParameter(keyValuePair.keykeyValuePair.value);
             }
 
             if (requestEntityFile.exists()) {
                 final byte[] entity = FileUtils.readFileToByteArray(requestEntityFile);
                 request.content(entity);
             }
 
         } finally {
             reader.close();
         }
     }
 
     private String readNextLine(final BufferedReader readerfinal String expectedValuethrows IOException {
         final String value = reader.readLine();
         if (!expectedValue.equals(value)) {
             throw new IllegalStateException("Unexpected value. Expected " + expectedValue + " but was " + value);
         }
         return value;
     }
 
     private String readKeyValuePairs(final BufferedReader readerfinal List<KeyValuePairpropertiesthrows IOException {
         String newLine = null;
         while ((newLine = reader.readLine()) != null) {
             final int equalSignIndex = newLine.indexOf("=");
             if (equalSignIndex != -1) {
                 final KeyValuePair pair = new KeyValuePair();
                 if (newLine.length() > equalSignIndex + 1) {
                     pair.key = newLine.substring(0, equalSignIndex);
                    pair.value = newLine.substring(equalSignIndex + 1);
                } else {
                    pair.key = newLine.substring(0, equalSignIndex);
                    pair.value = "";
                }
                properties.add(pair);
            } else {
                return newLine;
            }
        }
        return null;
    }
    private class KeyValuePair {
        public String key;
        public String value;
    }
New to GrepCode? Check out our FAQ X