/*
 * RESTHeart Common
 * 
 * Copyright (C) SoftInstigate Srl
 * 
 * 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.restheart.handlers.exchange;

import io.undertow.connector.PooledByteBuffer;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.AttachmentKey;
import io.undertow.util.Headers;
import java.io.IOException;
import java.lang.reflect.Field;

/**
 *
 * @author Andrea Di Cesare <andrea@softinstigate.com>
 * @param <T>
 */
public abstract class ProxableRequest<T> extends Request<T> {
    public ProxableRequest(HttpServerExchange exchange) {
        super(exchange);
    }
    
    public abstract T readContent() throws IOException;

    public abstract void writeContent(T content) throws IOException;

    protected void setContentLength(int length) {
        wrapped.getRequestHeaders().put(Headers.CONTENT_LENGTH, length);
    }

    @SuppressWarnings("unchecked")
    protected AttachmentKey<PooledByteBuffer[]> getRawContentKey() {
        Field f;

        try {
            f = HttpServerExchange.class.getDeclaredField("BUFFERED_REQUEST_DATA");
            f.setAccessible(true);
        } catch (NoSuchFieldException | SecurityException ex) {
            throw new RuntimeException("could not find BUFFERED_REQUEST_DATA field", ex);
        }

        try {
            return (AttachmentKey<PooledByteBuffer[]>) f.get(getWrapped());
        } catch (IllegalArgumentException | IllegalAccessException ex) {
            throw new RuntimeException("could not access BUFFERED_REQUEST_DATA field", ex);
        }
    }
    
    
    public void setRawContent(PooledByteBuffer[] raw) {
        getWrapped().putAttachment(getRawContentKey(), raw);
    }
    
    public PooledByteBuffer[] getRawContent() {
        if (!isContentAvailable()) {
            throw new IllegalStateException("Request content is not available. "
                    + "Add a Request Inteceptor with "
                    + "@RegisterPlugin(requiresContent = true) to make "
                    + "the content available.");
        }

        return getWrapped().getAttachment(getRawContentKey());
    }
    
    public boolean isContentAvailable() {
        return null != getWrapped().getAttachment(getRawContentKey());
    }
}

@@ -794,41 +794,8 @@
 ey;%0A
-import io.undertow.util.Headers;%0A
 impo
@@ -1218,138 +1218,8 @@
 n;%0A%0A
-    protected void setContentLength(int length) %7B%0A        wrapped.getRequestHeaders().put(Headers.CONTENT_LENGTH, length);%0A    %7D%0A%0A
     

Remove redundant method override