Hi again,
Again for the MTOM web service load test i am facing this exception on the
client side:
Failed to perform get operation.
{com.ardic.arcsp.update.wsclient.WSStorageServiceClient}
org.apache.axis2.AxisFault: org.apache.axiom.ext.io.StreamCopyException: Error
reading from source
at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)
at
com.ardic.arcsp.carbon.registry.ws.stub.WSStorageServiceStub.fromOM(WSStorageServiceStub.java:16706)
at
com.ardic.arcsp.carbon.registry.ws.stub.WSStorageServiceStub.wSgetById(WSStorageServiceStub.java:6659)
at
com.ardic.arcsp.update.wsclient.WSStorageServiceClient.getResourceById(WSStorageServiceClient.java:171)
at
com.ardic.arcsp.update.wsclient.OTAUpdateTestServlet.downloadResource(OTAUpdateTestServlet.java:139)
at
com.ardic.arcsp.update.wsclient.OTAUpdateTestServlet.doGet(OTAUpdateTestServlet.java:91)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.wso2.carbon.server.TomcatServer$1.invoke(TomcatServer.java:241)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at
org.apache.coyote.http11.Http11NioProcessor.process(Http11NioProcessor.java:396)
at
org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:356)
at
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1534)
at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.apache.axiom.om.OMException:
org.apache.axiom.ext.io.StreamCopyException: Error reading from source
at
org.apache.axiom.attachments.impl.PartFactory.createPart(PartFactory.java:172)
at
org.apache.axiom.attachments.Attachments.getPart(Attachments.java:705)
at
org.apache.axiom.attachments.Attachments.getNextPartDataHandler(Attachments.java:623)
at
org.apache.axiom.attachments.Attachments.getDataHandler(Attachments.java:350)
at
org.apache.axiom.om.impl.builder.OMAttachmentAccessorMimePartProvider.getDataHandler(OMAttachmentAccessorMimePartProvider.java:45)
at
org.apache.axiom.util.stax.xop.XOPDecodingStreamReader$DataHandlerProviderImpl.getDataHandler(XOPDecodingStreamReader.java:81)
at
org.apache.axiom.util.stax.xop.XOPDecodingStreamReader.getDataHandler(XOPDecodingStreamReader.java:472)
at
org.apache.axiom.om.impl.SwitchingWrapper.getDataHandler(SwitchingWrapper.java:1360)
at
org.apache.axiom.util.stax.XMLStreamReaderUtils.getDataHandlerFromElement(XMLStreamReaderUtils.java:230)
at
com.ardic.arcsp.carbon.registry.ws.stub.xsd.WSResource$Factory.parse(WSResource.java:2087)
at
com.ardic.arcsp.carbon.registry.ws.stub.WSgetByIdResponse$Factory.parse(WSgetByIdResponse.java:417)
at
com.ardic.arcsp.carbon.registry.ws.stub.WSStorageServiceStub.fromOM(WSStorageServiceStub.java:16273)
... 23 more
Caused by: org.apache.axiom.ext.io.StreamCopyException: Error reading from
source
at
org.apache.axiom.attachments.utils.BAAOutputStream.readFrom(BAAOutputStream.java:114)
at
org.apache.axiom.attachments.impl.BufferUtils.inputStream2OutputStream(BufferUtils.java:76)
at
org.apache.axiom.attachments.impl.PartFactory.createPart(PartFactory.java:136)
... 34 more
Caused by: java.io.IOException: CRLF expected at end of chunk: 2/63
at
org.apache.commons.httpclient.ChunkedInputStream.readCRLF(ChunkedInputStream.java:207)
at
org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:219)
at
org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:176)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at
org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:108)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at
org.apache.axiom.om.util.DetachableInputStream.read(DetachableInputStream.java:147)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at java.io.PushbackInputStream.read(PushbackInputStream.java:169)
at
org.apache.axiom.attachments.BoundaryPushbackInputStream.readFromStream(BoundaryPushbackInputStream.java:114)
at
org.apache.axiom.attachments.BoundaryPushbackInputStream.read(BoundaryPushbackInputStream.java:248)
at
org.apache.axiom.attachments.MIMEBodyPartInputStream.read(MIMEBodyPartInputStream.java:87)
at
org.apache.axiom.attachments.utils.BAAOutputStream.readFrom(BAAOutputStream.java:112)
... 36 more
The client side for the web service WSStorageService consists of a servlet
which used WSStorageClient referenced from the
org.wso2.carbon.registry.ws.client.WSRegistryServiceClient. In the get request
the servlet request the resorce file from the web service and the end user can
download the file. In the attachment you can find both the servlet and web
service client code. This exception occurs in some cases during my load test on
the web service. What is the cause of this exception? If this related to my
code, what do i have to do the avoid this error in my code?
Fatih Güçlü Akkaya
Software Development Engineer
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.com) All Rights Reserved.
*
* 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 com.ardic.arcsp.update.wsclient;
import java.util.Map;
import javax.activation.DataHandler;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.authenticator.stub.AuthenticationAdminStub;
import com.ardic.arcsp.carbon.registry.ws.stub.WSStorageServiceStub;
import com.ardic.arcsp.carbon.registry.ws.stub.WSgetByIdRegistryExceptionException;
import com.ardic.arcsp.carbon.registry.ws.stub.xsd.WSResource;
public class WSStorageServiceClient {
private static final Log log = LogFactory
.getLog(WSStorageServiceClient.class);
private WSStorageServiceStub storageStub;
private AuthenticationAdminStub authStub;
private String cookie;
private String epr;
public WSStorageServiceClient(String backendServerURL, String cookie)
throws Exception {
epr = backendServerURL + WSStorageClientConstants.WS_STORAGE_SERVICE;
/*
* Since user provided the cookie no need to authenticate with username
* and password
*/
setCookie(cookie);
try {
storageStub = new WSStorageServiceStub(epr);
ServiceClient client = storageStub._getServiceClient();
Options options = client.getOptions();
options.setManageSession(true);
options.setProperty(
org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING,
cookie);
storageStub._getServiceClient()
.getOptions()
.setProperty(Constants.Configuration.ENABLE_MTOM,
Constants.VALUE_TRUE);
// Increase the time out when sending large attachments
storageStub._getServiceClient().getOptions()
.setTimeOutInMilliSeconds(1000000);
} catch (Exception axisFault) {
String msg = "Failed to initiate WSStorage Service client. "
+ axisFault.getMessage();
log.error(msg, axisFault);
throw new Exception(msg, axisFault);
}
}
public WSStorageServiceClient(String serverURL, String username,
String password,ConfigurationContext configContext)
throws Exception {
epr = serverURL + WSStorageClientConstants.WS_STORAGE_SERVICE;
try {
authenticate( serverURL, username, password);
storageStub = new WSStorageServiceStub( epr);
//stub = new WSStorageServiceStub(epr);
ServiceClient client = storageStub._getServiceClient();
Options options = client.getOptions();
options.setManageSession(true);
options.setProperty(
org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING,
cookie);
storageStub._getServiceClient()
.getOptions()
.setProperty(Constants.Configuration.ENABLE_MTOM,
Constants.VALUE_TRUE);
// Increase the time out when sending large attachments
storageStub._getServiceClient().getOptions()
.setTimeOutInMilliSeconds(100000);
options.setProperty(HTTPConstants.SO_TIMEOUT, new Integer(100000));
options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, new Integer(100000));
} catch (Exception axisFault) {
String msg = "Failed to initiate WSStorage Service client. "
+ axisFault.getMessage();
log.error(msg, axisFault);
throw new Exception(msg, axisFault);
}
}
public WSStorageServiceClient(String serverURL, String username,
String password, String backendServerURL,
ConfigurationContext configContext) throws Exception {
epr = backendServerURL + "DynamicWSStorageService";
try {
authenticate(serverURL, username, password);
storageStub = new WSStorageServiceStub(configContext, epr);
ServiceClient client = storageStub._getServiceClient();
Options options = client.getOptions();
options.setManageSession(true);
options.setProperty(
org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING,
cookie);
storageStub._getServiceClient()
.getOptions()
.setProperty(Constants.Configuration.ENABLE_MTOM,
Constants.VALUE_TRUE);
// Increase the time out when sending large attachments
storageStub._getServiceClient().getOptions()
.setTimeOutInMilliSeconds(1000000);
} catch (Exception axisFault) {
String msg = "Failed to initiate WSStorage Service client. "
+ axisFault.getMessage();
log.error(msg, axisFault);
throw new Exception(msg, axisFault);
}
}
public boolean authenticate( String serverURL,
String username, String password) throws Exception
{
String serviceEPR = serverURL + WSStorageClientConstants.WS_AUTH_SERVICE;
AuthenticationAdminStub authStub = new AuthenticationAdminStub(
serviceEPR);
ServiceClient client = authStub._getServiceClient();
Options options = client.getOptions();
options.setManageSession(true);
options.setTimeOutInMilliSeconds(100000);
try {
boolean result = authStub.login(username, password, serviceEPR);
if (result) {
cookie = (String) authStub._getServiceClient().getServiceContext()
.getProperty(HTTPConstants.COOKIE_STRING);
}
return result;
} catch (Exception e) {
String msg = "Error occurred while logging in";
throw new Exception(msg, e);
}
}
public WSResource getResourceById(String resourceId) throws Exception {
WSResource wsResource = null;
try {
wsResource = storageStub.wSgetById(resourceId);
} catch (WSgetByIdRegistryExceptionException e) {
String msg = "Failed to perform get operation. " + e.getMessage();
log.error(msg, e);
throw new Exception(msg, e);// TODO: handle exception
}
finally{
cleanup();
}
return wsResource;
}
public DataHandler getById(String resourceId, String resourceName) throws Exception {
DataHandler content;
try {
content = storageStub.getContentById(resourceId, resourceName);
} catch (Exception e) {
String msg = "Failed to perform get operation.";
log.error(msg, e);
throw new Exception(msg, e);// TODO: handle exception
}
return content;
}
public boolean resourceExists(String path) throws Exception {
try {
return storageStub.resourceExists(path);
} catch (Exception e) {
String msg = "Failed to perform resourceExists operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public String put(String path, DataHandler dataHandler) {
String string = null;
try
{
WSResource wsResource = new WSResource();
wsResource.setContentFile(dataHandler);
wsResource.setPath(path);
wsResource.setName(dataHandler.getName());
string = storageStub.wSput(path, wsResource);
return string;
} catch (Exception e) {
String msg = "Failed to perform put operation.";
log.error(msg, e);
}
finally{
cleanup();
}
return string;
}
public void delete(String path) throws Exception {
try {
storageStub.delete(path);
} catch (Exception e) {
String msg = "Failed to perform delete operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public String rename(String currentPath, String newName)
throws Exception {
try {
return storageStub.rename(currentPath, newName);
} catch (Exception e) {
String msg = "Failed to perform rename operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public String move(String currentPath, String newPath)
throws Exception {
try {
return storageStub.move(currentPath, newPath);
} catch (Exception e) {
String msg = "Failed to perform move operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public String copy(String currentPath, String targetPath)
throws Exception {
try {
return storageStub.copy(currentPath, targetPath);
} catch (Exception e) {
String msg = "Failed to perform copy operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void createVersion(String path) throws Exception {
try {
storageStub.createVersion(path);
} catch (Exception e) {
String msg = "Failed to perform createVersion operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public String[] getVersions(String path) throws Exception {
try {
return storageStub.getVersions(path);
} catch (Exception e) {
String msg = "Failed to perform getVersions operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void restoreVersion(String path) throws Exception {
try {
storageStub.restoreVersion(path);
} catch (Exception e) {
String msg = "Failed to perform restoreVersion operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void addAssociation(String resourcePath, String targetPath,
String associationType) throws Exception {
try {
storageStub.addAssociation(resourcePath, targetPath, associationType);
} catch (Exception e) {
String msg = "Failed to perform addAssociation operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void removeAssociation(String resourcePath, String targetPath,
String associationType) throws Exception {
try {
storageStub.removeAssociation(resourcePath, targetPath, associationType);
} catch (Exception e) {
String msg = "Failed to perform removeAssociation operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void applyTag(String resourcePath, String tag)
throws Exception {
try {
storageStub.applyTag(resourcePath, tag);
} catch (Exception e) {
String msg = "Failed to perform applyTag operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void removeTag(String path, String tag) throws Exception {
try {
storageStub.removeTag(path, tag);
} catch (Exception e) {
String msg = "Failed to perform removeTag operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void editComment(String commentPath, String text)
throws Exception {
try {
storageStub.editComment(commentPath, text);
} catch (Exception e) {
String msg = "Failed to perform editComment operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void rateResource(String resourcePath, int rating)
throws Exception {
try {
storageStub.rateResource(resourcePath, rating);
} catch (Exception e) {
String msg = "Failed to perform rateResource operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public float getAverageRating(String resourcePath) throws Exception {
try {
return storageStub.getAverageRating(resourcePath);
} catch (Exception e) {
String msg = "Failed to perform getAverageRating operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public int getRating(String path, String username) throws Exception {
try {
return storageStub.getRating(path, username);
} catch (Exception e) {
String msg = "Failed to perform getRating operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public String[] getAvailableAspects() {
try {
return storageStub.getAvailableAspects();
} catch (Exception e) {
String msg = "Failed to perform getAvailableAspects operation.";
log.error(msg, e);
return new String[0];
}
}
public void associateAspect(String resourcePath, String aspect)
throws Exception {
try {
storageStub.associateAspect(resourcePath, aspect);
} catch (Exception e) {
String msg = "Failed to perform associateAspect operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void invokeAspect(String resourcePath, String aspectName,
String action) throws Exception {
try {
storageStub.invokeAspectNoParam(resourcePath, aspectName, action);
} catch (Exception e) {
String msg = "Failed to perform invokeAspect operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void invokeAspect(String resourcePath, String aspectName,
String action, Map<String, String> parameters)
throws Exception {
throw new UnsupportedOperationException(
"invokeAspect with parameters is not supported.");
}
public String[] getAspectActions(String resourcePath, String aspectName)
throws Exception {
try {
return storageStub.getAspectActions(resourcePath, aspectName);
} catch (Exception e) {
String msg = "Failed to perform getAspectActions operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void createLink(String path, String target) throws Exception {
try {
storageStub.createLinkWithSubTarget(path, target, null);
} catch (Exception e) {
String msg = "Failed to perform createLink operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void createLink(String path, String target, String subTargetPath)
throws Exception {
try {
storageStub.createLinkWithSubTarget(path, target, subTargetPath);
} catch (Exception e) {
String msg = "Failed to perform createLink operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void removeLink(String path) throws Exception {
try {
storageStub.removeLink(path);
} catch (Exception e) {
String msg = "Failed to perform removeLink operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public String getEventingServiceURL(String path) throws Exception {
try {
return storageStub.getEventingServiceURL(path);
} catch (Exception e) {
String msg = "Failed to perform getEventingServiceURL operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void setEventingServiceURL(String path, String serviceURL)
throws Exception {
try {
storageStub.setEventingServiceURL(path, serviceURL);
} catch (Exception e) {
String msg = "Failed to perform setEventingServiceURL operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public boolean removeAspect(String name) throws Exception {
try {
return storageStub.removeAspect(name);
} catch (Exception e) {
String msg = "Failed to perform removeAspect operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public void removeComment(String comment) throws Exception {
try {
storageStub.removeComment(comment);
} catch (Exception e) {
String msg = "Failed to perform removeComment operation.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
public DataHandler getContentById(String resourceId, String resourceName)
throws Exception {
DataHandler data = storageStub.getContentById(resourceId, resourceName);
storageStub._getServiceClient().cleanupTransport();
storageStub.cleanup();
return data;
}
public String[] getCollectionContent(String path) throws Exception {
return storageStub.getCollectionContent(path);
}
public String[] getCollectionContentById(String collectionId)
throws Exception {
return storageStub.getCollectionContentById(collectionId);
}
public void setCookie(String cookie) {
this.cookie = cookie;
}
public String getCookie(){
return this.cookie;
}
public void setStub(WSStorageServiceStub stub) {
this.storageStub = stub;
}
public void setEpr(String epr) {
this.epr = epr;
}
private void cleanup(){
try {
if(storageStub!= null){
storageStub._getServiceClient().cleanup();
storageStub._getServiceClient().cleanupTransport();
storageStub.cleanup();
}
} catch (AxisFault e) {
log.info("Error cleaning the StorageService resources");
}
try {
if(authStub != null){
authStub._getServiceClient().cleanup();
authStub._getServiceClient().cleanupTransport();
authStub.cleanup();
}
} catch (AxisFault e) {
log.info("Error cleaning the AuthenticationAdmin resources");
}
storageStub = null;
authStub = null;
}
}
package com.ardic.arcsp.update.wsclient;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.activation.DataHandler;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource;
import com.ardic.arcsp.carbon.registry.ws.stub.xsd.WSResource;
import com.ardic.arcsp.update.wsclient.WSStorageClientConstants;
import com.ardic.arcsp.update.wsclient.WSStorageServiceClient;
public class OTAUpdateTestServlet extends HttpServlet {
private Log log = LogFactory.getLog(OTAUpdateTestServlet.class);
private String username;
private String password;
private String resourceId;
private String resourceName;
private String resourcePath;
private ConfigurationContext configurationContext;
private static WSStorageServiceClient client;
private void _init() throws Exception {
// configurationContext = ConfigurationContextFactory
// .createConfigurationContextFromFileSystem(
// StorageServletConstants.AXIS2REPO,
// StorageServletConstants.AXIS2CONF);
// TODO SSL communication suppor will be added
// System.setProperty("javax.net.ssl.trustStore",
// CarbonUtils.getCarbonHome() +
// "/repository/resources/security/wso2carbon.jks");
// System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
// System.setProperty("javax.net.ssl.trustStoreType", "JKS");
client = new WSStorageServiceClient(
WSStorageClientConstants.ARCSP_ESB_EPR, username, password,
configurationContext);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
_init();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
username = req.getParameter("username");
password = req.getParameter("password");
resourceId = req.getParameter("resourceId");
// resourceName = req.getParameter("resourceName");
ServletOutputStream outputStream = null;
WSResource wsResource;
DataHandler data;
try {
wsResource = downloadResource(resourceId);
data = wsResource.getContentFile();
if (wsResource.getMediaType() != null)
resp.setContentType(wsResource.getMediaType());
else
resp.setContentType("application/download");
resp.setHeader("Content-Disposition", "attachment; filename=\""
+ wsResource.getName() + "\"");
synchronized (data) {
log.info("Request for file is processed");
outputStream = resp.getOutputStream();
data.writeTo(outputStream);
outputStream.flush();
resp.flushBuffer();
log.info("Request for file process ends.Data is written to the response");
}
} catch (Exception e) {
String msg = "Error Downloading the update: ";
throw new ServletException(msg + e.getMessage());
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}
// private DataHandler downloadResource(String resourceId, String resourceName)
// throws RemoteException, ServletException {
//
// DataHandler data = null;
// try {
// data = client.getContentById(resourceId, resourceName);
// } catch (Exception e1) {
// throw new ServletException("Error during downloading resource"
// + e1.getMessage());
// }
//
// return data;
//
// }
private WSResource downloadResource(String resourceId) throws ServletException{
WSResource resource = null;
try{
resource = client.getResourceById(resourceId);
}catch(Exception e){
throw new ServletException("Error during downloading resource" + e.getMessage());
}
return resource;
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
_init();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String mediaType = "";
String resourceId = "";
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
PrintWriter writer = null;
InputStream stream = null;
if (isMultipart)
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
stream = item.openStream();
if (item.isFormField()) {
if (name.equals("userName")) {
this.username = Streams.asString(stream);
} else if (name.equals("password")) {
this.password = Streams.asString(stream);
} else if (name.equals("resourcePath"))
this.resourcePath = Streams.asString(stream);
} else {
System.out.println("File field " + name
+ " with file name " + item.getName()
+ " detected.");
mediaType = item.getContentType();
this.resourceName = item.getName();
resourceId = uploadResource(this.resourcePath,
this.resourceName, stream, mediaType);
}
this.log.info("ResourceID is " + resourceId);
writer = resp.getWriter();
resp.setContentType("text/plain");
writer.write(resourceId);
}
} catch (ServletException ex) {
throw ex;
} catch (FileUploadException e) {
throw new ServletException("Error during uploading resource: "
+ e.getMessage());
}
}
private String uploadResource(String resourcePath, String resourceName,
InputStream stream, String mediaType) throws AxisFault,
ServletException {
//
// ByteArrayOutputStream outputStream = null;
String id = null;
// byte[] data = (byte[]) null;
try {
String destinationPath = resourcePath + File.separator
+ resourceName;
// outputStream = new ByteArrayOutputStream();
// byte[] chunkData = new byte[1024];
// int byteCount;
// while ((byteCount = stream.read(chunkData)) != -1) {
// outputStream.write(chunkData, 0, byteCount);
// }
InputStreamDataSource dataSource = new InputStreamDataSource(
stream, mediaType);
DataHandler dataContent = new DataHandler(dataSource);
id = client.put(destinationPath, dataContent);
} catch (Exception e) {
throw new ServletException("Error during uploading resource"
+ e.getMessage());
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
throw new ServletException("Error during closing streams"
+ e.getMessage());
}
}
// if (outputStream != null) {
// try {
// outputStream.close();
// } catch (IOException e) {
// throw new ServletException("Error during closing streams "
// + e.getMessage());
// }
// }
}
return id;
}
}
_______________________________________________
Carbon-dev mailing list
[email protected]
http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev