Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 JBoss Inc
   *
   * 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 io.apiman.test.common.mock;
 
 import java.util.Map;
 
 
A simple echo response POJO.

Author(s):
eric.wittmann@redhat.com
 
 public class EchoResponse {

    
Create an echo response from the inbound information in the http server request.

Parameters:
request the request
withBody if request is with body
Returns:
a new echo response
 
     public static EchoResponse from(HttpServletRequest requestboolean withBody) {
         EchoResponse response = new EchoResponse();
         response.setMethod(request.getMethod());
         if (request.getQueryString() != null) {
             response.setResource(request.getRequestURI() + "?" + request.getQueryString()); //$NON-NLS-1$
         } else {
             response.setResource(request.getRequestURI());
         }
         response.setUri(request.getRequestURI());
         Enumeration<StringheaderNames = request.getHeaderNames();
         while (headerNames.hasMoreElements()) {
             String name = headerNames.nextElement();
             String value = request.getHeader(name);
             response.getHeaders().put(namevalue);
         }
         if (withBody) {
             long totalBytes = 0;
             InputStream is = null;
             try {
                 is = request.getInputStream();
                 MessageDigest sha1 = MessageDigest.getInstance("SHA1"); //$NON-NLS-1$
                 byte[] data = new byte[1024];
                 int read = 0;
                 while ((read = is.read(data)) != -1) {
                     sha1.update(data, 0, read);
                     totalBytes += read;
                 };
                 
                 byte[] hashBytes = sha1.digest();
                 StringBuffer sb = new StringBuffer();
                 for (int i = 0; i < hashBytes.lengthi++) {
                   sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
                 }
                 String fileHash = sb.toString();
                 
                 response.setBodyLength(totalBytes);
                 response.setBodySha1(fileHash);
             } catch (Exception e) {
                 throw new RuntimeException(e);
             } finally {
                 IOUtils.closeQuietly(is);
             }
         }
         return response;
     }
     
     private String method;
     private String resource;
     private String uri;
     private Map<StringStringheaders = new HashMap<>();
     private Long bodyLength;
     private String bodySha1;
     private Long counter;
    
    
Constructor.
 
     public EchoResponse() {
    }

    

Returns:
the method
    public String getMethod() {
        return ;
    }

    

Parameters:
method the method to set
    public void setMethod(String method) {
        this. = method;
    }

    

Returns:
the resource
    public String getResource() {
        return ;
    }

    

Parameters:
resource the resource to set
    public void setResource(String resource) {
        this. = resource;
    }

    

Returns:
the headers
    public Map<StringStringgetHeaders() {
        return ;
    }

    

Parameters:
headers the headers to set
    public void setHeaders(Map<StringStringheaders) {
        this. = headers;
    }

    

Returns:
the uri
    public String getUri() {
        return ;
    }

    

Parameters:
uri the uri to set
    public void setUri(String uri) {
        this. = uri;
    }

    

Returns:
the bodyLength
    public Long getBodyLength() {
        return ;
    }

    

Parameters:
bodyLength the bodyLength to set
    public void setBodyLength(Long bodyLength) {
        this. = bodyLength;
    }

    

Returns:
the bodySha1
    public String getBodySha1() {
        return ;
    }

    

Parameters:
bodySha1 the bodySha1 to set
    public void setBodySha1(String bodySha1) {
        this. = bodySha1;
    }
    
    public Long getCounter() {
        return ;
    }
    public void setCounter(Long counter) {
        this. = counter;
    }
    
New to GrepCode? Check out our FAQ X