forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEC2MainServlet.java
More file actions
141 lines (125 loc) · 5.26 KB
/
Copy pathEC2MainServlet.java
File metadata and controls
141 lines (125 loc) · 5.26 KB
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
// 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 com.cloud.bridge.service;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.UUID;
import javax.inject.Inject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import com.cloud.bridge.persist.dao.CloudStackConfigurationDao;
import com.cloud.bridge.util.ConfigurationHelper;
import com.cloud.utils.LogUtils;
import com.cloud.utils.component.ComponentContext;
import com.cloud.utils.db.DB;
@Component("EC2MainServlet")
@DB
public class EC2MainServlet extends HttpServlet {
private static final long serialVersionUID = 2201599478145974479L;
public static final String EC2_REST_SERVLET_PATH = "/rest/AmazonEC2/";
public static final String EC2_SOAP_SERVLET_PATH = "/services/AmazonEC2/";
public static final String ENABLE_EC2_API = "enable.ec2.api";
private static boolean isEC2APIEnabled = false;
public static final Logger logger = Logger.getLogger(EC2MainServlet.class);
@Inject
CloudStackConfigurationDao csDao;
public EC2MainServlet() {
}
/**
* We build the path to where the keystore holding the WS-Security X509 certificates
* are stored.
*/
@Override
@DB
public void init(ServletConfig config) throws ServletException {
try {
LogUtils.initLog4j("log4j-cloud.xml");
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
ConfigurationHelper.preConfigureConfigPathFromServletContext(config.getServletContext());
ComponentContext.initComponentsLifeCycle();
// check if API is enabled
String value = csDao.getConfigValue(ENABLE_EC2_API);
if (value != null) {
isEC2APIEnabled = Boolean.valueOf(value);
}
logger.info("Value of EC2 API Flag ::" + value);
} catch (Exception e) {
throw new ServletException("Error initializing awsapi: " + e.getMessage(), e);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
doGetOrPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
doGetOrPost(req, resp);
}
protected void doGetOrPost(HttpServletRequest request, HttpServletResponse response) {
String action = request.getParameter("Action");
if (!isEC2APIEnabled) {
//response.sendError(404, "EC2 API is disabled.");
response.setStatus(404);
faultResponse(response, "Unavailable", "EC2 API is disabled");
return;
}
if (action != null) {
//We presume it's a Query/Rest call
try {
RequestDispatcher dispatcher = request.getRequestDispatcher(EC2_REST_SERVLET_PATH);
dispatcher.forward(request, response);
} catch (ServletException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
try {
request.getRequestDispatcher(EC2_SOAP_SERVLET_PATH).forward(request, response);
} catch (ServletException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private void faultResponse(HttpServletResponse response, String errorCode, String errorMessage) {
try {
OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream());
response.setContentType("text/xml; charset=UTF-8");
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.write("<Response><Errors><Error><Code>");
out.write(errorCode);
out.write("</Code><Message>");
out.write(errorMessage);
out.write("</Message></Error></Errors><RequestID>");
out.write(UUID.randomUUID().toString());
out.write("</RequestID></Response>");
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}