summaryrefslogtreecommitdiffstats
path: root/sandbox/slaws/domain-view/src/main/java/org/apache/tuscany/sca/domain/interop/Domain.java
blob: a8625e91a23d2255b0cddee64fec593a84a0002a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.tuscany.sca.domain.interop;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.oasisopen.sca.annotation.Service;
import org.apache.tuscany.sca.node.impl.NodeImpl;

/**
 * Some hand crafted HTTP code to help me think about what info is missing
 * from the domain ragistry
 *  
 */
@Service(Domain.class)
public class Domain implements Servlet {
    public String getDomainComposite(){
        NodeImpl node = (NodeImpl)Tuscany.node;
        String domainComposite = node.dumpDomainComposite();
        return domainComposite;
    }
    
    public String getComponent(){
        NodeImpl node = (NodeImpl)Tuscany.node;
        String domainComposite = node.dumpDomainComposite();
        return domainComposite;
    }    
    
    public void init(ServletConfig config) throws ServletException {
    }

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        ServletOutputStream os = response.getOutputStream();
        
        String pathInfo = httpRequest.getPathInfo();
        
        if (pathInfo == null || pathInfo.length() < 1){
            os.print("<html><body><p>URL should include at least a domain name, e.g. http://localhost/sca/mydomain</body></html>");
            return;
        }
        
        pathInfo = pathInfo.substring(1);
        String[] pathElements = pathInfo.split("/");
        
        String domainName = pathElements[0];
        String action = "none";
        
        if (pathElements.length == 2){
            action = pathElements[1];
        }
        
        // Just some hand crafted code to help me visualize resources
        os.print("<html><body>");
        
        if ( action.equals("composite")){
            String domainComposite = getDomainComposite();
            domainComposite = domainComposite.replaceAll("<", "&lt;");
            domainComposite = domainComposite.replaceAll(">", "&gt;");
            os.print("<html><body><p>" + domainComposite + "</body></html>");
            return;
        } else {       
            try {
                File domainDir = new File("target/test-classes/" + domainName);
                
                URI domainDirURI = domainDir.toURI();
                URI domainResourceURI = domainDirURI.resolve(pathInfo);
                
                os.print("<p/>contributions");
                
                for (File subDir : domainDir.listFiles()){
                    if (!subDir.isDirectory()){
                        os.print("<p/><a href=\"" + subDir.toURI().toString() + "\">" + subDir.getName());
                    }
                }
                
                os.print("<p/>nodes");
                os.print("<p/><a href=\"" + domainName + "/composite\">domainComposite</a>");
                
                os.print("</body></html>");
                           
                return;
            } catch (Exception ex){
                ex.printStackTrace();
            }
        }
        
        os.print("<html><body><p>no processing</body></html>");
        
    }

    public void destroy() {
    }

    public ServletConfig getServletConfig() {
        return null;
    }

    public String getServletInfo() {
        return null;
    }
    
    public void writeResource(ServletOutputStream os, URL url) throws Exception {
        URLConnection connection = url.openConnection();
        connection.setUseCaches(false);
        
        InputStream is = connection.getInputStream();
        int aChar;
        while(( aChar = is.read()) != -1){
            os.write(aChar);
        }
    }
       
}