Package org.apache.tuscany.sca.common.http

Examples of org.apache.tuscany.sca.common.http.HTTPContext


       
    }

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HTTPContext bindingContext = new HTTPContext();
        bindingContext.setHttpRequest(request);
        bindingContext.setHttpResponse(response);
       
        // Dispatch the service interaction to the service invoker
        Message requestMessage = messageFactory.createMessage();
        requestMessage.setBindingContext(bindingContext);
        requestMessage.setBody(new Object[]{request, response});
View Full Code Here


        this.next = next;
    }

    public Message invoke(Message msg) {
        try {
            HTTPContext bindingContext = (HTTPContext)msg.getBindingContext();
           
            // By-pass the operation selector
            if (bindingContext == null) {
                return getNext().invoke(msg);
            }

            String path = URLDecoder.decode(HTTPUtils.getRequestPath(bindingContext.getHttpRequest()), "UTF-8");

            if (path.startsWith("/")) {
                path = path.substring(1);
            }

            List<Operation> operations =
                filterOperationsByHttpMethod(interfaceContract, bindingContext.getHttpRequest().getMethod());

            Operation operation = findOperation(path, operations);

            final JavaOperation javaOperation = (JavaOperation)operation;
            final Method method = javaOperation.getJavaMethod();

            if (path != null && path.length() > 0) {
                if (method.getAnnotation(Path.class) != null) {
                    msg.setBody(new Object[] {path});
                }
            }

            // FIXME: [rfeng] We should follow JAX-RS rules to identify the entity parameter
            Class<?>[] paramTypes = method.getParameterTypes();
            if (paramTypes.length == 1) {
                Class<?> type = paramTypes[0];
                InputStream is = (InputStream)((Object[])msg.getBody())[0];
                Object target = convert(is, bindingContext.getHttpRequest().getContentType(), type);
                msg.setBody(new Object[] {target});
            } else if (paramTypes.length == 0) {
                msg.setBody(null);
            }
View Full Code Here

        this.messageFactory = messageFactory;
    }
   
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HTTPContext bindingContext = new HTTPContext();
        bindingContext.setHttpRequest(request);
        bindingContext.setHttpResponse(response);
        Message msg = messageFactory.createMessage();
        msg.setBindingContext(bindingContext);
        wire.invoke(msg);
    }   
View Full Code Here

    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if( binding.getOperationSelector() != null || binding.getRequestWireFormat() != null) {
            HTTPContext bindingContext = new HTTPContext();
            bindingContext.setHttpRequest(request);
            bindingContext.setHttpResponse(response);

            // Dispatch the service interaction to the service invoker
            Message requestMessage = messageFactory.createMessage();
            requestMessage.setBindingContext(bindingContext);
View Full Code Here

    public void setNext(Invoker next) {
        this.next = next;
    }

    public Message invoke(Message msg) {
        HTTPContext bindingContext = (HTTPContext) msg.getBindingContext();
       
        // Decode using the charset in the request if it exists otherwise
        // use UTF-8 as this is what all browser implementations use.
        String charset = bindingContext.getHttpRequest().getCharacterEncoding();
        if (charset == null) {
            charset = "UTF-8";
        }
       
        try {
View Full Code Here

        this.next = next;
    }

    public Message invoke(Message msg) {
        try {
            HTTPContext bindingContext = (HTTPContext)msg.getBindingContext();
           
            // By-pass the operation selector
            if (bindingContext == null) {
                return getNext().invoke(msg);
            }

            String path = URLDecoder.decode(HTTPUtil.getRequestPath(bindingContext.getHttpRequest()), "UTF-8");

            if (path.startsWith("/")) {
                path = path.substring(1);
            }

            List<Operation> operations =
                filterOperationsByHttpMethod(interfaceContract, bindingContext.getHttpRequest().getMethod());

            Operation operation = findOperation(path, operations);

            final JavaOperation javaOperation = (JavaOperation)operation;
            final Method method = javaOperation.getJavaMethod();

            if (path != null && path.length() > 0) {
                if (method.getAnnotation(Path.class) != null) {
                    msg.setBody(new Object[] {path});
                }
            }

            // FIXME: [rfeng] We should follow JAX-RS rules to identify the entity parameter
            Class<?>[] paramTypes = method.getParameterTypes();
            if (paramTypes.length == 1) {
                Class<?> type = paramTypes[0];
                InputStream is = (InputStream)((Object[])msg.getBody())[0];
                Object target = convert(is, bindingContext.getHttpRequest().getContentType(), type);
                msg.setBody(new Object[] {target});
            } else if (paramTypes.length == 0) {
                msg.setBody(null);
            }
View Full Code Here

    public void setNext(Invoker next) {
        this.next = next;
    }
   
    public Message invoke(Message msg) {
        HTTPContext bindingContext = (HTTPContext) msg.getBindingContext();
        if (bindingContext == null) {
            return getNext().invoke(msg);
        }
        // Decode using the charset in the request if it exists otherwise
        // use UTF-8 as this is what all browser implementations use.
        String charset = bindingContext.getHttpRequest().getCharacterEncoding();
        if (charset == null) {
            charset = "UTF-8";
        }
       
        try {
            if( isPayloadSupported(bindingContext.getHttpRequest().getMethod()) && msg.getBody() != null) {
                Object[] args = msg.getBody();
                InputStream in = (InputStream) args[0];
                String data = read(in, charset);
                JSONObject jsonPayload = new JSONObject(data);
                msg.setBody(new Object[]{jsonPayload});
View Full Code Here

        this.next = next;
    }

    public Message invoke(Message msg) {
        try {
            HTTPContext bindingContext = (HTTPContext)msg.getBindingContext();

            if(! "get".equalsIgnoreCase(bindingContext.getHttpRequest().getMethod())) {
                throw new RuntimeException("RPC Invocation only allowed over HTTP GET operations");
            }

            String path = URLDecoder.decode(HTTPUtil.getRequestPath(bindingContext.getHttpRequest()), "UTF-8");

            if (path.startsWith("/")) {
                path = path.substring(1);
            }
           

            String operationName = bindingContext.getHttpRequest().getParameter("method");
            Operation operation = findOperation( operationName );
           
            if (operation == null) {
                throw new RuntimeException("Invalid Operation '" + operationName + "'" );
            }

            final JavaOperation javaOperation = (JavaOperation)operation;
            final Method method = javaOperation.getJavaMethod();

            List<Object> messageParameters = new ArrayList<Object>();
            for(int i=0; i<method.getParameterTypes().length; i++) {
                for(Annotation annotation : method.getParameterAnnotations()[i]) {
                    if (annotation instanceof QueryParam) {
                        QueryParam queryParam = (QueryParam) annotation;
                        String name = queryParam.value();
                        String[] values = bindingContext.getHttpRequest().getParameterValues(name);
                        if(values.length == 1) {
                            messageParameters.add(values[0]);
                        } else {
                            messageParameters.add(values);
                        }
                    }
                }
            }
           
            Object[] body = new Object[messageParameters.size()];
            messageParameters.toArray(body);
           
            msg.setBody(body);
            msg.setOperation(operation);

            Message responseMessage = getNext().invoke(msg);
           
            //set Cache-Control to no-cache to avoid intermediary
            //proxy/reverse-proxy caches and always hit the server
            //that would identify if the value was current or not
            bindingContext.getHttpResponse().setHeader("Cache-Control", "no-cache");
            bindingContext.getHttpResponse().setHeader("Expires", new Date(0).toGMTString());
           
           
            String eTag = HTTPUtil.calculateHashETag(responseMessage.getBody().toString().getBytes("UTF-8"));
           
            // Test request for predicates.
            String predicate = bindingContext.getHttpRequest().getHeader( "If-Match" );
            if (( predicate != null ) && ( !predicate.equals(eTag) )) {
                // No match, should short circuit
                bindingContext.getHttpResponse().sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            }
            predicate = bindingContext.getHttpRequest().getHeader( "If-None-Match" );
            if (( predicate != null ) && ( predicate.equals(eTag) )) {
                // Match, should short circuit
                bindingContext.getHttpResponse().sendError(HttpServletResponse.SC_NOT_MODIFIED);
            }
           
            bindingContext.getHttpResponse().addHeader("ETag", eTag);
           
            return responseMessage;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
View Full Code Here

       
    }

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HTTPContext bindingContext = new HTTPContext();
        bindingContext.setHttpRequest(request);
        bindingContext.setHttpResponse(response);
       
        // Dispatch the service interaction to the service invoker
        Message requestMessage = messageFactory.createMessage();
        requestMessage.setBindingContext(bindingContext);
        requestMessage.setBody(new Object[]{request, response});
View Full Code Here

        this.next = next;
    }

    public Message invoke(Message msg) {

        HTTPContext bindingContext = (HTTPContext)msg.getBindingContext();

        // By-pass the operation selector
        if (bindingContext == null) {
            return getNext().invoke(msg);
        }

        String path = null;
        try {
            path = URLDecoder.decode(HTTPUtils.getRequestPath(bindingContext.getHttpRequest()), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }

        if (path.startsWith("/")) {
            path = path.substring(1);
        }

        List<Operation> operations =
            filterOperationsByHttpMethod(interfaceContract, bindingContext.getHttpRequest().getMethod());

        Operation operation = findOperation(path, operations);

        final JavaOperation javaOperation = (JavaOperation)operation;
        final Method method = javaOperation.getJavaMethod();

        if (path != null && path.length() > 0) {
            if (method.getAnnotation(Path.class) != null) {
                msg.setBody(new Object[] {path});
            }
        }

        // FIXME: [rfeng] We should follow JAX-RS rules to identify the entity parameter
        Class<?>[] paramTypes = method.getParameterTypes();
        if (paramTypes.length == 1) {
            Class<?> type = paramTypes[0];
            InputStream is = (InputStream)((Object[])msg.getBody())[0];
            Object target = convert(is, bindingContext.getHttpRequest().getContentType(), type);
            msg.setBody(new Object[] {target});
        } else if (paramTypes.length == 0) {
            msg.setBody(null);
        }
View Full Code Here

TOP

Related Classes of org.apache.tuscany.sca.common.http.HTTPContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.