2016-01-26 73 views
1

我寫這驗證是否有在請求一個特定的鍵CustomHandler。如果密鑰丟失,處理程序應該向客戶端發送迴應,說明密鑰丟失。當一個獲取請求被髮送到服務器而沒有標題中的密鑰時,服務器會使用修改後的有效負載響應客戶端。但是,當向服務器發送發佈請求時,它會發回客戶端發送的原始有效負載。以下是使用修改後的有效載荷向客戶端返回響應的代碼。如何更換響應主體在WSO2 API管理1.10.0自定義處理

private void handleAuthenticationFailure(MessageContext messageContext, APISecurityException e){     

    SOAPBody body = messageContext.getEnvelope().getBody(); 

    for (Iterator itr = body.getChildElements(); itr.hasNext();) { 
     OMElement child = (OMElement) itr.next(); 
     child.detach(); 
    } 

    org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext). 
      getAxis2MessageContext(); 

    axis2MC.setProperty(Constants.Configuration.CONTENT_TYPE, "application/xml");   
    axis2MC.removeProperty("NO_ENTITY_BODY"); 
    axis2MC.setProperty("HTTP_SC", HttpStatus.SC_UNAUTHORIZED); 

    messageContext.setResponse(true); 
    messageContext.setProperty("RESPONSE", "true"); 
    messageContext.setTo(null);  

    messageContext.getEnvelope().getBody().addChild(getFaultPayload(e)); 

    Axis2Sender.sendBack(messageContext);   
} 

private OMElement getFaultPayload(APISecurityException e) { 
    OMFactory fac = OMAbstractFactory.getOMFactory(); 
    OMNamespace ns = fac.createOMNamespace(APISecurityConstants.API_SECURITY_NS, 
      APISecurityConstants.API_SECURITY_NS_PREFIX); 
    OMElement payload = fac.createOMElement("fault", ns); 

    OMElement errorCode = fac.createOMElement("code", ns); 
    errorCode.setText(String.valueOf(e.getErrorCode())); 
    OMElement errorMessage = fac.createOMElement("message", ns); 
    errorMessage.setText("Missing Credentials"); 
    OMElement errorDetail = fac.createOMElement("description", ns); 
    errorDetail.setText(e.getMessage()); 

    payload.addChild(errorCode); 
    payload.addChild(errorMessage); 
    payload.addChild(errorDetail); 
    return payload; 
} 

我錯過了什麼嗎?如何刪除發佈請求的原始有效負載併發回已修改的有效負載?謝謝你的幫助。

回答

0

我們有類似的要求:HTTP響應的主體有效載荷內替換的任意字符串。我們通過自定義調解來實現這一目標,但這種方法應該通過處理程序同樣有效。

我們避免使用XML解析,建築,和注射用突觸JsonUtil(org.apache.synapse.commons.json.JsonUtil),具體String jsonResponse = JsonUtil.jsonPayloadToString(axis2)讀取有效載荷體和JsonUtil.newJsonPayload(axis2, jsonResponse, true, true)寫。

如果需要的有效載荷是XML,你會想看看Utils.setFaultPayloadUtils.sendFault,可在其org.wso2.carbon.apimgt.gateway.handlers包在https://github.com/wso2/carbon-apimgt/blob/master/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/Utils.java找到。

public static void setFaultPayload(MessageContext messageContext, OMElement payload) { 
    org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext). 
      getAxis2MessageContext(); 
    JsonUtil.removeJsonPayload(axis2MC); 
    messageContext.getEnvelope().getBody().addChild(payload); 
    Map headers = (Map) axis2MC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); 
    String acceptType = (String) headers.get(HttpHeaders.ACCEPT); 
    //If an Accept header has been provided. 
    if(!StringUtils.isEmpty(acceptType) && !"*/*".equals(acceptType)){ 
     axis2MC.setProperty(Constants.Configuration.MESSAGE_TYPE, acceptType); 
    } 
} 

    public static void sendFault(MessageContext messageContext, int status) { 
    org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext). 
      getAxis2MessageContext(); 

    axis2MC.setProperty(NhttpConstants.HTTP_SC, status); 
    messageContext.setResponse(true); 
    messageContext.setProperty("RESPONSE", "true"); 
    messageContext.setTo(null);   
    axis2MC.removeProperty("NO_ENTITY_BODY"); 

    // Always remove the ContentType - Let the formatter do its thing 
    axis2MC.removeProperty(Constants.Configuration.CONTENT_TYPE); 
    Map headers = (Map) axis2MC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); 
    if (headers != null) { 
     headers.remove(HttpHeaders.AUTHORIZATION); 
     headers.remove(HttpHeaders.AUTHORIZATION); 

     headers.remove(HttpHeaders.HOST); 
    } 
    Axis2Sender.sendBack(messageContext); 
}