Commit ad7a719c by zhongmh

增加开发人员信息上传功能

parent d2f77847
......@@ -6,7 +6,7 @@
<groupId>cn.yunmaozj.tools</groupId>
<artifactId>maven-site-upload-parent</artifactId>
<version>1.0.3</version>
<version>1.0.5</version>
<description>站点上传插件</description>
<modules>
<module>site-upload-maven-plugin</module>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>maven-site-upload-parent</artifactId>
<groupId>cn.yunmaozj.tools</groupId>
<version>1.0.3</version>
<version>1.0.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -15,7 +15,6 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
public class SiteServer {
public static void main(String[] args) {
SpringApplication.run(SiteServer.class, args);
}
......
package cn.yunmaozj.common;
import java.util.List;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-19 14:13
*/
public class DeveloperEntity {
private String id;
private String email;
private String name;
private List<String> roles;
private String project;
}
......@@ -3,6 +3,7 @@ package cn.yunmaozj.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
......@@ -21,7 +22,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class);
@Bean
public UserDetailsService userDetailsService(){
public UserDetailsService userDetailsService() {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withDefaultPasswordEncoder().username("user").password("99boliyuan").roles("root").build());
return manager;
......@@ -30,5 +31,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();
http.headers().frameOptions().sameOrigin();
http.authorizeRequests()
.antMatchers(HttpMethod.POST, "/site-upload", "/update-developer");
}
}
package cn.yunmaozj.controller;
import cn.yunmaozj.common.DeveloperEntity;
import cn.yunmaozj.common.SystemFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import java.io.IOException;
import java.util.List;
/**
* @author zhongminghong
......@@ -26,4 +29,12 @@ public class UploadController {
public void siteUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
systemFile.unzip(multipartFile.getResource().getFilename(), multipartFile.getInputStream());
}
/**
* 更新开发人员
*/
@PostMapping("/update-developer")
public void updateDeveloper(@RequestBody List<DeveloperEntity> entities) {
}
}
......@@ -3,6 +3,7 @@ package cn.yunmaozj.security;
import cn.yunmaozj.common.SystemFile;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
......@@ -48,6 +49,7 @@ public class Developer {
private final static String TAG_PROJECTS = "projects";
private final static String TAG_PROJECT = "project";
private final static String PASSWORD = "password";
private final static String EMAIL = "email";
private final static String DEFAULT_CHARSET = "UTF-8";
private final static OutputFormat outputFormat;
......@@ -121,7 +123,6 @@ public class Developer {
document.addComment("开发人员信息");
String dateString = DateFormatUtils.format(new Date(), DEFAULT_DATE_FORMAT);
element.addAttribute(NAME, name);
element.addAttribute(PASSWORD, PASSWORD);
element.addAttribute(CREATE_DATE, dateString);
element.addAttribute(UPDATE_DATE, dateString);
element.addAttribute(LAST_LOGIN_DATE, dateString);
......@@ -191,6 +192,35 @@ public class Developer {
}
}
private String getAttribute(String name) {
String value = null;
try {
if (StringUtils.isEmpty(name)) {
throw new NullPointerException("password == null");
}
Document document = saxReader.read(userFile);
Attribute attribute = document.getRootElement().attribute(name);
value = attribute.getValue();
write(document);
} catch (DocumentException e) {
e.printStackTrace();
}
return value;
}
public void setEmail(String email) {
try {
if (StringUtils.isEmpty(email)) {
throw new NullPointerException("email == null");
}
Document document = saxReader.read(userFile);
document.getRootElement().addAttribute(EMAIL, email);
write(document);
} catch (DocumentException e) {
e.printStackTrace();
}
}
private synchronized void write(Document document) {
XMLWriter writer = null;
try {
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>maven-site-upload-parent</artifactId>
<groupId>cn.yunmaozj.tools</groupId>
<version>1.0.3</version>
<version>1.0.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......@@ -68,6 +68,11 @@
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- dependencies to annotations -->
<dependency>
......
package cn.yunmaozj.upload;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import jdk.nashorn.api.scripting.JSObject;
import jdk.nashorn.internal.ir.debug.JSONWriter;
import okhttp3.*;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.maven.model.Developer;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
......@@ -59,6 +65,8 @@ public class SiteUploadMojo extends AbstractMojo {
@Parameter(defaultValue = "${settings}", readonly = true, required = true)
private Settings settings;
private OkHttpClient client;
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
getLog().info("skip site-upload");
......@@ -71,13 +79,18 @@ public class SiteUploadMojo extends AbstractMojo {
return;
}
getLog().info("输入目录:" + inputDirectory);
String name = StringUtils.isEmpty(directory.getName()) ? project.getArtifactId() : directory.getName();
try {
String name = StringUtils.isEmpty(directory.getName()) ? project.getArtifactId() : directory.getName();
//上传压缩文件
File outFile = compress(name, inputDirectory);
uploader(name, outFile);
//上传开发人员信息
List<Developer> developers = project.getDevelopers();
uploadDeveloper(developers, name);
} catch (IOException e) {
e.printStackTrace();
}
}
}
......@@ -91,30 +104,13 @@ public class SiteUploadMojo extends AbstractMojo {
if (outFile.isDirectory()) {
throw new MojoExecutionException("上传的文件[" + outFile.getPath() + "]的类型是一个目录");
}
OkHttpClient.Builder builder = new OkHttpClient.Builder();
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), outFile);
MultipartBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", name, fileBody)
.build();
if (StringUtils.isNotEmpty(siteServerUserName) && StringUtils.isNotEmpty(siteServerPassword)) {
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.proxyAuthenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
if (response.request().header("Proxy-Authorization") != null) {
getLog().error("response.request().header(\"Proxy-Authorization\") != null");
return null;
}
String credential = Credentials.basic(proxyUserName, proxyPassword);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
});
}
OkHttpClient client = builder.build();
HttpUrl.Builder httpUrlBuilder = new HttpUrl.Builder();
if (ssl) {
httpUrlBuilder.scheme("https");
......@@ -128,11 +124,56 @@ public class SiteUploadMojo extends AbstractMojo {
httpUrlBuilder.addPathSegment("site-upload");
Request request = new Request.Builder().url(httpUrlBuilder.build())
.post(body).build();
Response response = client.newCall(request).execute();
Response response = getClient().newCall(request).execute();
getLog().info("Uploading file:[" + outFile + "] to [" + request.url() + "]");
getLog().info(response.toString());
}
public void uploadDeveloper(List<Developer> developers, String name) throws IOException {
if (developers == null || developers.size() == 0) {
getLog().debug("developer.size == 0");
return;
}
JsonArray developerArray = new JsonArray();
for (Developer developer : developers) {
JsonObject developerObject = new JsonObject();
if (StringUtils.isEmpty(developer.getEmail())) {
throw new NullPointerException("开发人员必须设置email属性");
}
if (StringUtils.isEmpty(developer.getName())) {
throw new NullPointerException("开发人员必须设置name属性");
}
developerObject.addProperty("id", developer.getId());
developerObject.addProperty("email", developer.getEmail());
developerObject.addProperty("name", developer.getName());
JsonArray roleArray = new JsonArray();
if (developer.getRoles() == null || developer.getRoles().size() == 0) {
roleArray.add("developer");
} else {
developer.getRoles().forEach(R -> roleArray.add(R));
}
developerObject.add("roles", roleArray);
developerObject.addProperty("project", name);
developerArray.add(developerObject);
}
RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), developerArray.getAsString());
HttpUrl.Builder httpUrlBuilder = new HttpUrl.Builder();
if (ssl) {
httpUrlBuilder.scheme("https");
} else {
httpUrlBuilder.scheme("http");
}
httpUrlBuilder.host(siteServerHost).port(siteServerPort);
if (StringUtils.isNotEmpty(siteServerUserName) && StringUtils.isNotEmpty(siteServerPassword)) {
httpUrlBuilder.username(siteServerUserName).password(siteServerPassword);
}
httpUrlBuilder.addPathSegment("site-upload");
Request request = new Request.Builder().url(httpUrlBuilder.build())
.post(body).build();
Response response = getClient().newCall(request).execute();
getLog().info("upload developer info [" + response.toString() + "]");
}
/**
* 压缩文件
*/
......@@ -175,4 +216,32 @@ public class SiteUploadMojo extends AbstractMojo {
return outFile;
}
public synchronized OkHttpClient getClient() {
if (client != null) {
return client;
}
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (StringUtils.isNotEmpty(siteServerUserName) && StringUtils.isNotEmpty(siteServerPassword)) {
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.proxyAuthenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
if (response.request().header("Proxy-Authorization") != null) {
getLog().error("response.request().header(\"Proxy-Authorization\") != null");
return null;
}
String credential = Credentials.basic(proxyUserName, proxyPassword);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
});
}
OkHttpClient client = builder.build();
this.client = client;
return client;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment